@builtbyfriends/realtime-ai 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present, Built By Friends Company
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,11 @@
1
- # Temporary Holding Version
1
+ # @builtbyfriends/realtime-ai
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ AI tool definitions (Anthropic API, AI SDK) for an realtime registry.
4
+
5
+ It's part of [Realtime](https://github.com/builtbyfriends/realtime), a procedure builder for Cloudflare Workers.
6
+
7
+ ```sh
8
+ npm install @builtbyfriends/realtime-ai
9
+ ```
10
+
11
+ MIT license.
@@ -0,0 +1,54 @@
1
+ import type { CallResult, Exec, JsonSchema, Registry } from '@builtbyfriends/realtime';
2
+ export type ToolInfo = {
3
+ name: string;
4
+ procedure: string;
5
+ description: string;
6
+ needsApproval: boolean;
7
+ inputSchema: JsonSchema;
8
+ };
9
+ /** Every procedure that chose `.aiTool()`, with its JSON Schema. */
10
+ export declare function toolInfo(registry: Registry<any>): ToolInfo[];
11
+ /** Tool definitions in the Anthropic Messages API shape (`tools: [...]`). */
12
+ export declare function anthropicTools(registry: Registry<any>): {
13
+ name: string;
14
+ description: string;
15
+ input_schema: JsonSchema;
16
+ }[];
17
+ export type ToolRunResult = {
18
+ status: 'ok';
19
+ result: CallResult;
20
+ } | {
21
+ status: 'needs_approval';
22
+ tool: string;
23
+ input: unknown;
24
+ } | {
25
+ status: 'unknown_tool';
26
+ tool: string;
27
+ };
28
+ /**
29
+ * Runs one tool call through the full pipeline. The caller is whoever sent `exec.request`,
30
+ * so an agent acting for a user gets exactly that user's permissions. A tool that chose
31
+ * `needsApproval: true` returns `needs_approval` until the caller passes `approved: true`.
32
+ */
33
+ export declare function runTool(registry: Registry<any>, toolName: string, input: unknown, exec: Omit<Exec, 'surface'> & {
34
+ approved?: boolean;
35
+ }): Promise<ToolRunResult>;
36
+ /** The `content` string for a tool_result block: the value on success, the error otherwise. */
37
+ export declare function toolResultContent(run: ToolRunResult): {
38
+ content: string;
39
+ is_error: boolean;
40
+ };
41
+ /**
42
+ * Tools for the Vercel AI SDK. Pass its `jsonSchema` helper so this package needs no
43
+ * dependency on `ai`: `aiSdkTools(registry, exec, { jsonSchema })`.
44
+ * Tools that chose `needsApproval: true` set the AI SDK's `needsApproval` flag.
45
+ */
46
+ export declare function aiSdkTools<S>(registry: Registry<any>, exec: Omit<Exec, 'surface'>, helpers: {
47
+ jsonSchema: (schema: any) => S;
48
+ }): Record<string, {
49
+ description: string;
50
+ inputSchema: S;
51
+ needsApproval: boolean;
52
+ execute: (input: unknown) => Promise<unknown>;
53
+ }>;
54
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+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M5F;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,EACvB,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,EAC3B,OAAO,EAAE;IAAE,UAAU,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAA;CAAE;iBAEA,MAAM;iBAAe,CAAC;mBAAiB,OAAO;aAAW,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC;GAezI"}
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ function aiProcedures(registry) {
2
+ const out = [];
3
+ for (const p of registry.procedures.values()) {
4
+ const t = p.decisions.aiTool;
5
+ if (t.kind !== 'tool')
6
+ continue;
7
+ out.push({
8
+ p,
9
+ info: {
10
+ name: p.toolName,
11
+ procedure: p.name,
12
+ description: t.description,
13
+ needsApproval: t.needsApproval,
14
+ inputSchema: p.jsonSchema.input ?? { type: 'object', properties: {} },
15
+ },
16
+ });
17
+ }
18
+ return out;
19
+ }
20
+ /** Every procedure that chose `.aiTool()`, with its JSON Schema. */
21
+ export function toolInfo(registry) {
22
+ return aiProcedures(registry).map((x) => x.info);
23
+ }
24
+ /** Tool definitions in the Anthropic Messages API shape (`tools: [...]`). */
25
+ export function anthropicTools(registry) {
26
+ return toolInfo(registry).map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema }));
27
+ }
28
+ /**
29
+ * Runs one tool call through the full pipeline. The caller is whoever sent `exec.request`,
30
+ * so an agent acting for a user gets exactly that user's permissions. A tool that chose
31
+ * `needsApproval: true` returns `needs_approval` until the caller passes `approved: true`.
32
+ */
33
+ export async function runTool(registry, toolName, input, exec) {
34
+ const found = aiProcedures(registry).find((x) => x.info.name === toolName);
35
+ const gate = !found ? 'unknown-tool' : !found.info.needsApproval ? 'no-approval-needed' : exec.approved ? 'approved' : 'needs-approval';
36
+ // Lets dev tools show the approval gate next to the call it guards.
37
+ if (registry.trace?.active)
38
+ registry.trace.emit({ type: 'tool-gate', at: Date.now(), tool: toolName, ...(found ? { procedure: found.p.name } : {}), request: exec.request, input, gate });
39
+ if (!found)
40
+ return { status: 'unknown_tool', tool: toolName };
41
+ if (gate === 'needs-approval')
42
+ return { status: 'needs_approval', tool: toolName, input };
43
+ const { approved: _a, ...rest } = exec;
44
+ return { status: 'ok', result: await registry.call(found.p.name, input, { ...rest, surface: 'ai' }) };
45
+ }
46
+ /** The `content` string for a tool_result block: the value on success, the error otherwise. */
47
+ export function toolResultContent(run) {
48
+ if (run.status === 'needs_approval')
49
+ return { content: JSON.stringify({ error: { code: 'NEEDS_APPROVAL', message: 'A person must approve this call first' } }), is_error: true };
50
+ if (run.status === 'unknown_tool')
51
+ return { content: JSON.stringify({ error: { code: 'UNKNOWN_TOOL', message: `No tool ${run.tool}` } }), is_error: true };
52
+ const r = run.result;
53
+ if (r.ok)
54
+ return { content: JSON.stringify(r.value), is_error: false };
55
+ return { content: JSON.stringify({ error: { code: r.code, message: r.message, ...(r.issues ? { issues: r.issues } : {}) } }), is_error: true };
56
+ }
57
+ /**
58
+ * Tools for the Vercel AI SDK. Pass its `jsonSchema` helper so this package needs no
59
+ * dependency on `ai`: `aiSdkTools(registry, exec, { jsonSchema })`.
60
+ * Tools that chose `needsApproval: true` set the AI SDK's `needsApproval` flag.
61
+ */
62
+ export function aiSdkTools(registry, exec, helpers) {
63
+ const tools = {};
64
+ for (const { p, info } of aiProcedures(registry)) {
65
+ tools[info.name] = {
66
+ description: info.description,
67
+ inputSchema: helpers.jsonSchema(info.inputSchema),
68
+ needsApproval: info.needsApproval,
69
+ // The AI SDK only calls execute after its own approval step, so approval is granted here.
70
+ execute: async (input) => {
71
+ const r = await registry.call(p.name, input, { ...exec, surface: 'ai' });
72
+ if (r.ok)
73
+ return r.value;
74
+ return { error: { code: r.code, message: r.message, ...(r.issues ? { issues: r.issues } : {}) } };
75
+ },
76
+ };
77
+ }
78
+ return tools;
79
+ }
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G;AACH,MAAM,UAAU,UAAU,CACxB,QAAuB,EACvB,IAA2B,EAC3B,OAA2C;IAE3C,MAAM,KAAK,GAAmI,EAAE,CAAC;IACjJ,KAAK,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC;QACjD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YACjB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW,EAAE,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC;YACjD,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,0FAA0F;YAC1F,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;gBACvB,MAAM,CAAC,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;gBACzE,IAAI,CAAC,CAAC,EAAE;oBAAE,OAAO,CAAC,CAAC,KAAK,CAAC;gBACzB,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACpG,CAAC;SACF,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@builtbyfriends/realtime-ai",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "AI tool definitions (Anthropic API, AI SDK) for an realtime registry",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@builtbyfriends/realtime": "0.1.1"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/builtbyfriends/realtime.git",
24
+ "directory": "typescript/packages/ai"
25
+ },
26
+ "homepage": "https://github.com/builtbyfriends/realtime#readme",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "scripts": {
31
+ "prepack": "cp ../../../LICENSE LICENSE"
32
+ }
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,100 @@
1
+ import type { CallResult, Exec, JsonSchema, LoadedProcedure, Registry } from '@builtbyfriends/realtime';
2
+
3
+ export type ToolInfo = {
4
+ name: string;
5
+ procedure: string;
6
+ description: string;
7
+ needsApproval: boolean;
8
+ inputSchema: JsonSchema;
9
+ };
10
+
11
+ function aiProcedures(registry: Registry<any>): { p: LoadedProcedure; info: ToolInfo }[] {
12
+ const out: { p: LoadedProcedure; info: ToolInfo }[] = [];
13
+ for (const p of registry.procedures.values()) {
14
+ const t = p.decisions.aiTool;
15
+ if (t.kind !== 'tool') continue;
16
+ out.push({
17
+ p,
18
+ info: {
19
+ name: p.toolName,
20
+ procedure: p.name,
21
+ description: t.description,
22
+ needsApproval: t.needsApproval,
23
+ inputSchema: p.jsonSchema.input ?? { type: 'object', properties: {} },
24
+ },
25
+ });
26
+ }
27
+ return out;
28
+ }
29
+
30
+ /** Every procedure that chose `.aiTool()`, with its JSON Schema. */
31
+ export function toolInfo(registry: Registry<any>): ToolInfo[] {
32
+ return aiProcedures(registry).map((x) => x.info);
33
+ }
34
+
35
+ /** Tool definitions in the Anthropic Messages API shape (`tools: [...]`). */
36
+ export function anthropicTools(registry: Registry<any>) {
37
+ return toolInfo(registry).map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema }));
38
+ }
39
+
40
+ export type ToolRunResult =
41
+ | { status: 'ok'; result: CallResult }
42
+ | { status: 'needs_approval'; tool: string; input: unknown }
43
+ | { status: 'unknown_tool'; tool: string };
44
+
45
+ /**
46
+ * Runs one tool call through the full pipeline. The caller is whoever sent `exec.request`,
47
+ * so an agent acting for a user gets exactly that user's permissions. A tool that chose
48
+ * `needsApproval: true` returns `needs_approval` until the caller passes `approved: true`.
49
+ */
50
+ export async function runTool(
51
+ registry: Registry<any>,
52
+ toolName: string,
53
+ input: unknown,
54
+ exec: Omit<Exec, 'surface'> & { approved?: boolean },
55
+ ): Promise<ToolRunResult> {
56
+ const found = aiProcedures(registry).find((x) => x.info.name === toolName);
57
+ const gate = !found ? 'unknown-tool' : !found.info.needsApproval ? 'no-approval-needed' : exec.approved ? 'approved' : 'needs-approval';
58
+ // Lets dev tools show the approval gate next to the call it guards.
59
+ if (registry.trace?.active) registry.trace.emit({ type: 'tool-gate', at: Date.now(), tool: toolName, ...(found ? { procedure: found.p.name } : {}), request: exec.request, input, gate });
60
+ if (!found) return { status: 'unknown_tool', tool: toolName };
61
+ if (gate === 'needs-approval') return { status: 'needs_approval', tool: toolName, input };
62
+ const { approved: _a, ...rest } = exec;
63
+ return { status: 'ok', result: await registry.call(found.p.name, input, { ...rest, surface: 'ai' }) };
64
+ }
65
+
66
+ /** The `content` string for a tool_result block: the value on success, the error otherwise. */
67
+ export function toolResultContent(run: ToolRunResult): { content: string; is_error: boolean } {
68
+ if (run.status === 'needs_approval') return { content: JSON.stringify({ error: { code: 'NEEDS_APPROVAL', message: 'A person must approve this call first' } }), is_error: true };
69
+ if (run.status === 'unknown_tool') return { content: JSON.stringify({ error: { code: 'UNKNOWN_TOOL', message: `No tool ${run.tool}` } }), is_error: true };
70
+ const r = run.result;
71
+ if (r.ok) return { content: JSON.stringify(r.value), is_error: false };
72
+ return { content: JSON.stringify({ error: { code: r.code, message: r.message, ...(r.issues ? { issues: r.issues } : {}) } }), is_error: true };
73
+ }
74
+
75
+ /**
76
+ * Tools for the Vercel AI SDK. Pass its `jsonSchema` helper so this package needs no
77
+ * dependency on `ai`: `aiSdkTools(registry, exec, { jsonSchema })`.
78
+ * Tools that chose `needsApproval: true` set the AI SDK's `needsApproval` flag.
79
+ */
80
+ export function aiSdkTools<S>(
81
+ registry: Registry<any>,
82
+ exec: Omit<Exec, 'surface'>,
83
+ helpers: { jsonSchema: (schema: any) => S },
84
+ ) {
85
+ const tools: Record<string, { description: string; inputSchema: S; needsApproval: boolean; execute: (input: unknown) => Promise<unknown> }> = {};
86
+ for (const { p, info } of aiProcedures(registry)) {
87
+ tools[info.name] = {
88
+ description: info.description,
89
+ inputSchema: helpers.jsonSchema(info.inputSchema),
90
+ needsApproval: info.needsApproval,
91
+ // The AI SDK only calls execute after its own approval step, so approval is granted here.
92
+ execute: async (input) => {
93
+ const r = await registry.call(p.name, input, { ...exec, surface: 'ai' });
94
+ if (r.ok) return r.value;
95
+ return { error: { code: r.code, message: r.message, ...(r.issues ? { issues: r.issues } : {}) } };
96
+ },
97
+ };
98
+ }
99
+ return tools;
100
+ }