@xi-era/acp-adapter-openai 0.1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 xi-era (open-source community arm of Stellxis)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,48 @@
1
+ import { ComponentDescriptor, AcpClient } from '@xi-era/acp-sdk/client';
2
+
3
+ /**
4
+ * ACP -> OpenAI Tool-Call adapter (spec 附录 B).
5
+ * Zero runtime dependencies besides the SDK: pure functions plus a handler
6
+ * factory that executes model tool_calls against an ACP client.
7
+ */
8
+
9
+ /** Minimal OpenAI tool-call types (deliberately inline — no OpenAI SDK needed). */
10
+ interface OpenAiToolDef {
11
+ type: "function";
12
+ function: {
13
+ name: string;
14
+ description: string;
15
+ parameters: object;
16
+ };
17
+ }
18
+ interface OpenAiToolCall {
19
+ id: string;
20
+ type?: "function";
21
+ function: {
22
+ name: string;
23
+ arguments: string;
24
+ };
25
+ }
26
+ interface OpenAiToolMessage {
27
+ role: "tool";
28
+ tool_call_id: string;
29
+ content: string;
30
+ }
31
+ /**
32
+ * Converts ACP component descriptors to OpenAI function tool definitions.
33
+ * name = component_id with "." -> "_" (spec §7.1 lossless mapping);
34
+ * parameters = the descriptor's draft-07 inputSchema, passed through as-is.
35
+ */
36
+ declare function componentsToOpenaiTools(descriptors: ComponentDescriptor[]): OpenAiToolDef[];
37
+ interface ToolCallHandlerOptions {
38
+ /** Max tool messages produced per invocation; default unlimited. */
39
+ limit?: number;
40
+ }
41
+ /**
42
+ * Creates a handler for the `tool_calls` array of an OpenAI assistant message.
43
+ * Each call's JSON arguments are parsed and used as the ACP `input`;
44
+ * the ACP result is returned serialized as the tool message content.
45
+ */
46
+ declare function createToolCallHandler(client: AcpClient, options?: ToolCallHandlerOptions): (toolCalls: OpenAiToolCall[]) => Promise<OpenAiToolMessage[]>;
47
+
48
+ export { type OpenAiToolCall, type OpenAiToolDef, type OpenAiToolMessage, type ToolCallHandlerOptions, componentsToOpenaiTools, createToolCallHandler };
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ // src/index.ts
2
+ import { componentIdToToolName } from "@xi-era/acp-sdk/client";
3
+ function componentsToOpenaiTools(descriptors) {
4
+ return descriptors.map((d) => ({
5
+ type: "function",
6
+ function: {
7
+ name: componentIdToToolName(d.id),
8
+ description: d.description,
9
+ parameters: d.inputSchema ?? { type: "object", properties: {}, required: [] }
10
+ }
11
+ }));
12
+ }
13
+ function createToolCallHandler(client, options = {}) {
14
+ return async (toolCalls) => {
15
+ const calls = options.limit ? toolCalls.slice(0, options.limit) : toolCalls;
16
+ return Promise.all(
17
+ calls.map(async (toolCall) => {
18
+ const componentId = toolCall.function.name.replaceAll("_", ".");
19
+ let input;
20
+ try {
21
+ input = toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : null;
22
+ } catch {
23
+ return {
24
+ role: "tool",
25
+ tool_call_id: toolCall.id,
26
+ content: `error: arguments is not valid JSON: ${toolCall.function.arguments}`
27
+ };
28
+ }
29
+ try {
30
+ const result = await client.call(componentId, input);
31
+ return { role: "tool", tool_call_id: toolCall.id, content: JSON.stringify(result) };
32
+ } catch (e) {
33
+ const message = e instanceof Error ? e.message : String(e);
34
+ return { role: "tool", tool_call_id: toolCall.id, content: `error: ${message}` };
35
+ }
36
+ })
37
+ );
38
+ };
39
+ }
40
+ export {
41
+ componentsToOpenaiTools,
42
+ createToolCallHandler
43
+ };
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * ACP -> OpenAI Tool-Call adapter (spec 附录 B).\n * Zero runtime dependencies besides the SDK: pure functions plus a handler\n * factory that executes model tool_calls against an ACP client.\n */\nimport type { ComponentDescriptor } from \"@xi-era/acp-sdk/client\";\nimport type { AcpClient } from \"@xi-era/acp-sdk/client\";\nimport { componentIdToToolName } from \"@xi-era/acp-sdk/client\";\n\n/** Minimal OpenAI tool-call types (deliberately inline — no OpenAI SDK needed). */\nexport interface OpenAiToolDef {\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: object;\n };\n}\n\nexport interface OpenAiToolCall {\n id: string;\n type?: \"function\";\n function: { name: string; arguments: string };\n}\n\nexport interface OpenAiToolMessage {\n role: \"tool\";\n tool_call_id: string;\n content: string;\n}\n\n/**\n * Converts ACP component descriptors to OpenAI function tool definitions.\n * name = component_id with \".\" -> \"_\" (spec §7.1 lossless mapping);\n * parameters = the descriptor's draft-07 inputSchema, passed through as-is.\n */\nexport function componentsToOpenaiTools(descriptors: ComponentDescriptor[]): OpenAiToolDef[] {\n return descriptors.map((d) => ({\n type: \"function\" as const,\n function: {\n name: componentIdToToolName(d.id),\n description: d.description,\n parameters: d.inputSchema ?? { type: \"object\", properties: {}, required: [] },\n },\n }));\n}\n\nexport interface ToolCallHandlerOptions {\n /** Max tool messages produced per invocation; default unlimited. */\n limit?: number;\n}\n\n/**\n * Creates a handler for the `tool_calls` array of an OpenAI assistant message.\n * Each call's JSON arguments are parsed and used as the ACP `input`;\n * the ACP result is returned serialized as the tool message content.\n */\nexport function createToolCallHandler(\n client: AcpClient,\n options: ToolCallHandlerOptions = {}\n): (toolCalls: OpenAiToolCall[]) => Promise<OpenAiToolMessage[]> {\n return async (toolCalls) => {\n const calls = options.limit ? toolCalls.slice(0, options.limit) : toolCalls;\n return Promise.all(\n calls.map(async (toolCall): Promise<OpenAiToolMessage> => {\n const componentId = toolCall.function.name.replaceAll(\"_\", \".\");\n let input: unknown;\n try {\n input = toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : null;\n } catch {\n return {\n role: \"tool\",\n tool_call_id: toolCall.id,\n content: `error: arguments is not valid JSON: ${toolCall.function.arguments}`,\n };\n }\n try {\n const result = await client.call(componentId, input);\n return { role: \"tool\", tool_call_id: toolCall.id, content: JSON.stringify(result) };\n } catch (e) {\n const message = e instanceof Error ? e.message : String(e);\n return { role: \"tool\", tool_call_id: toolCall.id, content: `error: ${message}` };\n }\n })\n );\n };\n}\n"],"mappings":";AAOA,SAAS,6BAA6B;AA6B/B,SAAS,wBAAwB,aAAqD;AAC3F,SAAO,YAAY,IAAI,CAAC,OAAO;AAAA,IAC7B,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,sBAAsB,EAAE,EAAE;AAAA,MAChC,aAAa,EAAE;AAAA,MACf,YAAY,EAAE,eAAe,EAAE,MAAM,UAAU,YAAY,CAAC,GAAG,UAAU,CAAC,EAAE;AAAA,IAC9E;AAAA,EACF,EAAE;AACJ;AAYO,SAAS,sBACd,QACA,UAAkC,CAAC,GAC4B;AAC/D,SAAO,OAAO,cAAc;AAC1B,UAAM,QAAQ,QAAQ,QAAQ,UAAU,MAAM,GAAG,QAAQ,KAAK,IAAI;AAClE,WAAO,QAAQ;AAAA,MACb,MAAM,IAAI,OAAO,aAAyC;AACxD,cAAM,cAAc,SAAS,SAAS,KAAK,WAAW,KAAK,GAAG;AAC9D,YAAI;AACJ,YAAI;AACF,kBAAQ,SAAS,SAAS,YAAY,KAAK,MAAM,SAAS,SAAS,SAAS,IAAI;AAAA,QAClF,QAAQ;AACN,iBAAO;AAAA,YACL,MAAM;AAAA,YACN,cAAc,SAAS;AAAA,YACvB,SAAS,uCAAuC,SAAS,SAAS,SAAS;AAAA,UAC7E;AAAA,QACF;AACA,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,KAAK,aAAa,KAAK;AACnD,iBAAO,EAAE,MAAM,QAAQ,cAAc,SAAS,IAAI,SAAS,KAAK,UAAU,MAAM,EAAE;AAAA,QACpF,SAAS,GAAG;AACV,gBAAM,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC;AACzD,iBAAO,EAAE,MAAM,QAAQ,cAAc,SAAS,IAAI,SAAS,UAAU,OAAO,GAAG;AAAA,QACjF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@xi-era/acp-adapter-openai",
3
+ "version": "0.1.0",
4
+ "description": "ACP (Agent-Component-Protocol) to OpenAI Tool-Call adapter — convert ACP component descriptors to OpenAI function tools and execute tool calls against ACP servers.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "keywords": [
22
+ "acp",
23
+ "openai",
24
+ "tool-call",
25
+ "adapter",
26
+ "agent"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/xi-era/acp-protocol.git",
31
+ "directory": "packages/acp-adapter-openai"
32
+ },
33
+ "homepage": "https://github.com/xi-era/acp-protocol",
34
+ "dependencies": {
35
+ "@xi-era/acp-sdk": "0.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.7.4",
39
+ "tsup": "^8.3.0"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup"
43
+ }
44
+ }