@ui-calling/openai 0.0.0-stage → 0.1.0-preview.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 +21 -0
- package/README.md +18 -2
- package/dist/index.d.ts +61 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 San Pau
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ui-calling/openai
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ui-calling surfaces as function tools for OpenAI's Responses or Chat Completions API. No SDK dependency.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { openaiResponsesTools, runResponsesFunctionCall } from "@ui-calling/openai";
|
|
7
|
+
|
|
8
|
+
const response = await openai.responses.create({ model, input: convo.items, tools: openaiResponsesTools(ui) });
|
|
9
|
+
convo.items.push(...response.output); // the model's own output goes back before the results
|
|
10
|
+
for (const call of response.output) {
|
|
11
|
+
if (call.type !== "function_call") continue;
|
|
12
|
+
const { item, run } = await runResponsesFunctionCall(ui, call, who); // never throws
|
|
13
|
+
convo.items.push(item); // the function_call_output
|
|
14
|
+
if (run.ok) links.push(run.link); // deliver it as a button
|
|
15
|
+
}
|
|
16
|
+
// call responses.create again with convo.items until there's no function_call
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Chat Completions: `openaiChatTools(ui)` and `runChatToolCall(ui, toolCall, who)`, which returns `{ message, run }`. The whole loop: `npx ui-calling guide agent`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { UiCalling, Who, ToolRun } from '@ui-calling/core';
|
|
2
|
+
|
|
3
|
+
type Filter = (toolName: string) => boolean;
|
|
4
|
+
/** `woken`: a screen event started this turn, not a user message, so `stay_silent` is offered too. The run then has `run.silent`. */
|
|
5
|
+
type Turn = {
|
|
6
|
+
woken?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/** Function tools for the Responses API: `client.responses.create({ tools })`. */
|
|
9
|
+
declare function openaiResponsesTools(ui: UiCalling, filter?: Filter, turn?: Turn): {
|
|
10
|
+
type: "function";
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
parameters: Record<string, unknown>;
|
|
14
|
+
strict: boolean;
|
|
15
|
+
}[];
|
|
16
|
+
/** Function tools for Chat Completions: `client.chat.completions.create({ tools })`. */
|
|
17
|
+
declare function openaiChatTools(ui: UiCalling, filter?: Filter, turn?: Turn): {
|
|
18
|
+
type: "function";
|
|
19
|
+
function: {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
parameters: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
}[];
|
|
25
|
+
/**
|
|
26
|
+
* Run a Responses API `function_call` output item. Returns the `function_call_output` item to add to
|
|
27
|
+
* the next request's input, and the run: deliver `run.link` when `run.ok`, send nothing this turn when `run.silent`.
|
|
28
|
+
*/
|
|
29
|
+
declare function runResponsesFunctionCall(ui: UiCalling, call: {
|
|
30
|
+
call_id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
arguments: string;
|
|
33
|
+
}, who: Who,
|
|
34
|
+
/** The filter you gave `openaiResponsesTools`: a tool the model wasn't offered is refused. */
|
|
35
|
+
filter?: Filter): Promise<{
|
|
36
|
+
item: {
|
|
37
|
+
type: "function_call_output";
|
|
38
|
+
call_id: string;
|
|
39
|
+
output: string;
|
|
40
|
+
};
|
|
41
|
+
run: ToolRun;
|
|
42
|
+
}>;
|
|
43
|
+
/** Run a Chat Completions tool call. Returns the `tool` message to append, and the run. */
|
|
44
|
+
declare function runChatToolCall(ui: UiCalling, call: {
|
|
45
|
+
id: string;
|
|
46
|
+
function: {
|
|
47
|
+
name: string;
|
|
48
|
+
arguments: string;
|
|
49
|
+
};
|
|
50
|
+
}, who: Who,
|
|
51
|
+
/** The filter you gave `openaiChatTools`: a tool the model wasn't offered is refused. */
|
|
52
|
+
filter?: Filter): Promise<{
|
|
53
|
+
message: {
|
|
54
|
+
role: "tool";
|
|
55
|
+
tool_call_id: string;
|
|
56
|
+
content: string;
|
|
57
|
+
};
|
|
58
|
+
run: ToolRun;
|
|
59
|
+
}>;
|
|
60
|
+
|
|
61
|
+
export { openaiChatTools, openaiResponsesTools, runChatToolCall, runResponsesFunctionCall };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function openaiResponsesTools(ui, filter, turn = {}) {
|
|
3
|
+
return ui.tools({ ...turn, filter }).map((t) => ({ type: "function", name: t.name, description: t.description, parameters: t.input_schema, strict: false }));
|
|
4
|
+
}
|
|
5
|
+
function openaiChatTools(ui, filter, turn = {}) {
|
|
6
|
+
return ui.tools({ ...turn, filter }).map((t) => ({
|
|
7
|
+
type: "function",
|
|
8
|
+
function: { name: t.name, description: t.description, parameters: t.input_schema }
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
async function runResponsesFunctionCall(ui, call, who, filter) {
|
|
12
|
+
const run = await runWithArguments(ui, call.name, call.arguments, who, filter);
|
|
13
|
+
return { item: { type: "function_call_output", call_id: call.call_id, output: run.content }, run };
|
|
14
|
+
}
|
|
15
|
+
async function runChatToolCall(ui, call, who, filter) {
|
|
16
|
+
const run = await runWithArguments(ui, call.function.name, call.function.arguments, who, filter);
|
|
17
|
+
return { message: { role: "tool", tool_call_id: call.id, content: run.content }, run };
|
|
18
|
+
}
|
|
19
|
+
async function runWithArguments(ui, name, args, who, filter) {
|
|
20
|
+
let input;
|
|
21
|
+
try {
|
|
22
|
+
input = JSON.parse(args || "{}");
|
|
23
|
+
} catch {
|
|
24
|
+
const error = { code: "invalid_arguments", message: "Tool arguments were not valid JSON" };
|
|
25
|
+
return { ok: false, content: JSON.stringify({ status: "error", ...error }), error };
|
|
26
|
+
}
|
|
27
|
+
return ui.runTool(name, input, who, filter);
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
openaiChatTools,
|
|
31
|
+
openaiResponsesTools,
|
|
32
|
+
runChatToolCall,
|
|
33
|
+
runResponsesFunctionCall
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ToolRun, UiCalling, Who } from \"@ui-calling/core\";\n\ntype Filter = (toolName: string) => boolean;\n/** `woken`: a screen event started this turn, not a user message, so `stay_silent` is offered too. The run then has `run.silent`. */\ntype Turn = { woken?: boolean };\n\n/** Function tools for the Responses API: `client.responses.create({ tools })`. */\nexport function openaiResponsesTools(ui: UiCalling, filter?: Filter, turn: Turn = {}) {\n return ui\n .tools({ ...turn, filter })\n .map((t) => ({ type: \"function\" as const, name: t.name, description: t.description, parameters: t.input_schema, strict: false }));\n}\n\n/** Function tools for Chat Completions: `client.chat.completions.create({ tools })`. */\nexport function openaiChatTools(ui: UiCalling, filter?: Filter, turn: Turn = {}) {\n return ui\n .tools({ ...turn, filter })\n .map((t) => ({\n type: \"function\" as const,\n function: { name: t.name, description: t.description, parameters: t.input_schema },\n }));\n}\n\n/**\n * Run a Responses API `function_call` output item. Returns the `function_call_output` item to add to\n * the next request's input, and the run: deliver `run.link` when `run.ok`, send nothing this turn when `run.silent`.\n */\nexport async function runResponsesFunctionCall(\n ui: UiCalling,\n call: { call_id: string; name: string; arguments: string },\n who: Who,\n /** The filter you gave `openaiResponsesTools`: a tool the model wasn't offered is refused. */\n filter?: Filter,\n): Promise<{ item: { type: \"function_call_output\"; call_id: string; output: string }; run: ToolRun }> {\n const run = await runWithArguments(ui, call.name, call.arguments, who, filter);\n return { item: { type: \"function_call_output\", call_id: call.call_id, output: run.content }, run };\n}\n\n/** Run a Chat Completions tool call. Returns the `tool` message to append, and the run. */\nexport async function runChatToolCall(\n ui: UiCalling,\n call: { id: string; function: { name: string; arguments: string } },\n who: Who,\n /** The filter you gave `openaiChatTools`: a tool the model wasn't offered is refused. */\n filter?: Filter,\n): Promise<{ message: { role: \"tool\"; tool_call_id: string; content: string }; run: ToolRun }> {\n const run = await runWithArguments(ui, call.function.name, call.function.arguments, who, filter);\n return { message: { role: \"tool\", tool_call_id: call.id, content: run.content }, run };\n}\n\n// OpenAI sends arguments as a JSON string; malformed JSON becomes an error result the model can fix.\nasync function runWithArguments(ui: UiCalling, name: string, args: string, who: Who, filter?: Filter): Promise<ToolRun> {\n let input: unknown;\n try {\n input = JSON.parse(args || \"{}\");\n } catch {\n const error = { code: \"invalid_arguments\", message: \"Tool arguments were not valid JSON\" };\n return { ok: false, content: JSON.stringify({ status: \"error\", ...error }), error };\n }\n return ui.runTool(name, input, who, filter);\n}\n"],"mappings":";AAOO,SAAS,qBAAqB,IAAe,QAAiB,OAAa,CAAC,GAAG;AACpF,SAAO,GACJ,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,EACzB,IAAI,CAAC,OAAO,EAAE,MAAM,YAAqB,MAAM,EAAE,MAAM,aAAa,EAAE,aAAa,YAAY,EAAE,cAAc,QAAQ,MAAM,EAAE;AACpI;AAGO,SAAS,gBAAgB,IAAe,QAAiB,OAAa,CAAC,GAAG;AAC/E,SAAO,GACJ,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,EACzB,IAAI,CAAC,OAAO;AAAA,IACX,MAAM;AAAA,IACN,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,EAAE,aAAa,YAAY,EAAE,aAAa;AAAA,EACnF,EAAE;AACN;AAMA,eAAsB,yBACpB,IACA,MACA,KAEA,QACoG;AACpG,QAAM,MAAM,MAAM,iBAAiB,IAAI,KAAK,MAAM,KAAK,WAAW,KAAK,MAAM;AAC7E,SAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,SAAS,KAAK,SAAS,QAAQ,IAAI,QAAQ,GAAG,IAAI;AACnG;AAGA,eAAsB,gBACpB,IACA,MACA,KAEA,QAC6F;AAC7F,QAAM,MAAM,MAAM,iBAAiB,IAAI,KAAK,SAAS,MAAM,KAAK,SAAS,WAAW,KAAK,MAAM;AAC/F,SAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,cAAc,KAAK,IAAI,SAAS,IAAI,QAAQ,GAAG,IAAI;AACvF;AAGA,eAAe,iBAAiB,IAAe,MAAc,MAAc,KAAU,QAAmC;AACtH,MAAI;AACJ,MAAI;AACF,YAAQ,KAAK,MAAM,QAAQ,IAAI;AAAA,EACjC,QAAQ;AACN,UAAM,QAAQ,EAAE,MAAM,qBAAqB,SAAS,qCAAqC;AACzF,WAAO,EAAE,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE,QAAQ,SAAS,GAAG,MAAM,CAAC,GAAG,MAAM;AAAA,EACpF;AACA,SAAO,GAAG,QAAQ,MAAM,OAAO,KAAK,MAAM;AAC5C;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ui-calling/openai",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-preview.1",
|
|
4
|
+
"description": "Use ui-calling surfaces as function tools with OpenAI's Responses or Chat Completions API. No SDK dependency.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/pausanchez/ui-calling.git",
|
|
9
|
+
"directory": "packages/openai"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/pausanchez/ui-calling#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/pausanchez/ui-calling/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"ui-calling",
|
|
17
|
+
"openai",
|
|
18
|
+
"tools"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"ui-calling-source": "./src/index.ts",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup"
|
|
36
|
+
},
|
|
37
|
+
"tsup": {
|
|
38
|
+
"entry": {
|
|
39
|
+
"index": "src/index.ts"
|
|
40
|
+
},
|
|
41
|
+
"format": [
|
|
42
|
+
"esm"
|
|
43
|
+
],
|
|
44
|
+
"dts": true,
|
|
45
|
+
"clean": true,
|
|
46
|
+
"sourcemap": true,
|
|
47
|
+
"target": "es2022",
|
|
48
|
+
"removeNodeProtocol": false
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@ui-calling/core": "^0.1.0-preview.1"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
}
|
|
56
|
+
}
|