@yolk-sdk/voice-runtime 0.0.1-canary.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 +21 -0
- package/README.md +75 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/tool-bridge.d.mts +24 -0
- package/dist/tool-bridge.d.mts.map +1 -0
- package/dist/tool-bridge.mjs +44 -0
- package/dist/tool-bridge.mjs.map +1 -0
- package/package.json +56 -0
- package/src/index.ts +6 -0
- package/src/tool-bridge.ts +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yolk SDK contributors
|
|
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
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @yolk-sdk/voice-runtime
|
|
2
|
+
|
|
3
|
+
Provider-neutral bridge from realtime voice tool calls to Yolk tool execution.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @yolk-sdk/voice-runtime@canary @yolk-sdk/agent@canary effect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
|
|
12
|
+
|
|
13
|
+
## Subpaths
|
|
14
|
+
|
|
15
|
+
| Subpath | Purpose |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| `@yolk-sdk/voice-runtime` | Voice tool request/result schemas and execution bridge |
|
|
18
|
+
|
|
19
|
+
## Imports
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { executeVoiceToolCall } from '@yolk-sdk/voice-runtime'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## What it provides
|
|
26
|
+
|
|
27
|
+
- Normalized voice tool call request/result types.
|
|
28
|
+
- `executeVoiceToolCall` for running calls through `ToolExecutor`.
|
|
29
|
+
- JSON string result envelope for realtime provider adapters.
|
|
30
|
+
- Typed voice bridge errors.
|
|
31
|
+
|
|
32
|
+
## Example
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { Effect } from 'effect'
|
|
36
|
+
import { executeVoiceToolCall } from '@yolk-sdk/voice-runtime'
|
|
37
|
+
|
|
38
|
+
const result = executeVoiceToolCall({
|
|
39
|
+
callId: 'call_1',
|
|
40
|
+
name: 'search',
|
|
41
|
+
arguments: '{"query":"hello"}'
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Host provides ToolExecutor.
|
|
45
|
+
Effect.runPromise(result)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Output is a provider-safe JSON string envelope:
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{ "result": { "content": [{ "type": "text", "text": "..." }] } }
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Failures use:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{ "error": "..." }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Use it when
|
|
61
|
+
|
|
62
|
+
- A realtime provider adapter receives a tool call and needs to execute it through Yolk tools.
|
|
63
|
+
|
|
64
|
+
## Host responsibilities
|
|
65
|
+
|
|
66
|
+
- Normalize provider tool-call events into this package's request shape.
|
|
67
|
+
- Provide `ToolExecutor` and app tool policy.
|
|
68
|
+
- Convert the JSON string envelope into provider-specific realtime responses.
|
|
69
|
+
|
|
70
|
+
## Boundaries
|
|
71
|
+
|
|
72
|
+
- No WebRTC or microphone code.
|
|
73
|
+
- No OpenAI Realtime SDK imports.
|
|
74
|
+
- No app tool catalogs.
|
|
75
|
+
- Provider adapters convert this package's result into provider-specific realtime events.
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import { ToolExecutor } from "@yolk-sdk/agent/loop";
|
|
4
|
+
|
|
5
|
+
//#region src/tool-bridge.d.ts
|
|
6
|
+
declare const VoiceToolCallRequest_base: Schema.Class<VoiceToolCallRequest, Schema.Struct<{
|
|
7
|
+
readonly callId: Schema.Trimmed;
|
|
8
|
+
readonly name: Schema.Trimmed;
|
|
9
|
+
readonly arguments: Schema.String;
|
|
10
|
+
}>, {}>;
|
|
11
|
+
declare class VoiceToolCallRequest extends VoiceToolCallRequest_base {}
|
|
12
|
+
declare const VoiceToolExecutionResult_base: Schema.Class<VoiceToolExecutionResult, Schema.Struct<{
|
|
13
|
+
readonly toolCallId: Schema.String;
|
|
14
|
+
readonly output: Schema.String;
|
|
15
|
+
}>, {}>;
|
|
16
|
+
declare class VoiceToolExecutionResult extends VoiceToolExecutionResult_base {}
|
|
17
|
+
declare const VoiceToolBridgeError_base: Schema.Class<VoiceToolBridgeError, Schema.TaggedStruct<"VoiceToolBridgeError", {
|
|
18
|
+
readonly message: Schema.String;
|
|
19
|
+
}>, import("effect/Cause").YieldableError>;
|
|
20
|
+
declare class VoiceToolBridgeError extends VoiceToolBridgeError_base {}
|
|
21
|
+
declare const executeVoiceToolCall: (input: VoiceToolCallRequest) => Effect.Effect<VoiceToolExecutionResult, never, ToolExecutor>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { VoiceToolBridgeError, VoiceToolCallRequest, VoiceToolExecutionResult, executeVoiceToolCall };
|
|
24
|
+
//# sourceMappingURL=tool-bridge.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-bridge.d.mts","names":[],"sources":["../src/tool-bridge.ts"],"mappings":";;;;;cAEmE,yBAAA;;;;;cAMtD,oBAAA,SAA6B,yBAMxC;AAAA,cAAG,6BAAA;;;;cAEQ,wBAAA,SAAiC,6BAK5C;AAAA,cAAG,yBAAA;;;cAEQ,oBAAA,SAA6B,yBAKzC;AAAA,cA6CY,oBAAA,GAAwB,KAAA,EAAO,oBAAA,KAAoB,MAAA,CAAA,MAAA,CAAA,wBAAA,SAAA,YAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import { ToolExecutor } from "@yolk-sdk/agent/loop";
|
|
4
|
+
import { ToolCall } from "@yolk-sdk/agent/protocol";
|
|
5
|
+
//#region src/tool-bridge.ts
|
|
6
|
+
const NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()));
|
|
7
|
+
const maxVoiceToolResultCharacters = 6e3;
|
|
8
|
+
var VoiceToolCallRequest = class extends Schema.Class("VoiceToolCallRequest")({
|
|
9
|
+
callId: NonEmptyTrimmedString,
|
|
10
|
+
name: NonEmptyTrimmedString,
|
|
11
|
+
arguments: Schema.String
|
|
12
|
+
}) {};
|
|
13
|
+
var VoiceToolExecutionResult = class extends Schema.Class("VoiceToolExecutionResult")({
|
|
14
|
+
toolCallId: Schema.String,
|
|
15
|
+
output: Schema.String
|
|
16
|
+
}) {};
|
|
17
|
+
var VoiceToolBridgeError = class extends Schema.TaggedErrorClass()("VoiceToolBridgeError", { message: Schema.String }) {};
|
|
18
|
+
const unknownToMessage = (error) => error instanceof Error ? error.message : String(error);
|
|
19
|
+
const parseToolArguments = (raw) => Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(raw).pipe(Effect.mapError((error) => new VoiceToolBridgeError({ message: `Invalid tool arguments JSON: ${unknownToMessage(error)}` })));
|
|
20
|
+
const stringifyToolOutput = (value) => Schema.encodeUnknownEffect(Schema.UnknownFromJsonString)(value).pipe(Effect.mapError((error) => new VoiceToolBridgeError({ message: `Could not serialize tool output: ${unknownToMessage(error)}` })));
|
|
21
|
+
const truncateVoiceToolResult = (value) => {
|
|
22
|
+
if (value.length <= maxVoiceToolResultCharacters) return value;
|
|
23
|
+
return `${value.slice(0, maxVoiceToolResultCharacters)}\n\n[truncated for voice; summarize from available excerpt]`;
|
|
24
|
+
};
|
|
25
|
+
const contentToSerializable = (content) => typeof content === "string" ? truncateVoiceToolResult(content) : content;
|
|
26
|
+
const makeVoiceToolExecutionResult = (toolCallId, output) => VoiceToolExecutionResult.make({
|
|
27
|
+
toolCallId,
|
|
28
|
+
output
|
|
29
|
+
});
|
|
30
|
+
const makeToolErrorResult = (toolCallId, error) => stringifyToolOutput({ error: error.message }).pipe(Effect.catchTag("VoiceToolBridgeError", () => Effect.succeed("{\"error\":\"Tool failed\"}")), Effect.map((output) => makeVoiceToolExecutionResult(toolCallId, output)));
|
|
31
|
+
const executeVoiceToolCall = (input) => Effect.gen(function* () {
|
|
32
|
+
const executor = yield* ToolExecutor;
|
|
33
|
+
const params = yield* parseToolArguments(input.arguments);
|
|
34
|
+
const output = yield* stringifyToolOutput({ result: contentToSerializable((yield* executor.execute(ToolCall.make({
|
|
35
|
+
id: input.callId,
|
|
36
|
+
name: input.name,
|
|
37
|
+
params
|
|
38
|
+
}))).content) });
|
|
39
|
+
return makeVoiceToolExecutionResult(input.callId, output);
|
|
40
|
+
}).pipe(Effect.catchTag("ToolError", (error) => makeToolErrorResult(input.callId, error)), Effect.catchTag("VoiceToolBridgeError", (error) => makeToolErrorResult(input.callId, error)));
|
|
41
|
+
//#endregion
|
|
42
|
+
export { VoiceToolBridgeError, VoiceToolCallRequest, VoiceToolExecutionResult, executeVoiceToolCall };
|
|
43
|
+
|
|
44
|
+
//# sourceMappingURL=tool-bridge.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-bridge.mjs","names":[],"sources":["../src/tool-bridge.ts"],"sourcesContent":["import { Effect } from 'effect'\nimport * as Schema from 'effect/Schema'\nimport { ToolExecutor, type ToolError } from '@yolk-sdk/agent/loop'\nimport { ToolCall, type Content } from '@yolk-sdk/agent/protocol'\n\nconst NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))\nconst maxVoiceToolResultCharacters = 6000\n\nexport class VoiceToolCallRequest extends Schema.Class<VoiceToolCallRequest>(\n 'VoiceToolCallRequest'\n)({\n callId: NonEmptyTrimmedString,\n name: NonEmptyTrimmedString,\n arguments: Schema.String\n}) {}\n\nexport class VoiceToolExecutionResult extends Schema.Class<VoiceToolExecutionResult>(\n 'VoiceToolExecutionResult'\n)({\n toolCallId: Schema.String,\n output: Schema.String\n}) {}\n\nexport class VoiceToolBridgeError extends Schema.TaggedErrorClass<VoiceToolBridgeError>()(\n 'VoiceToolBridgeError',\n {\n message: Schema.String\n }\n) {}\n\nconst unknownToMessage = (error: unknown) =>\n error instanceof Error ? error.message : String(error)\n\nconst parseToolArguments = (raw: string) =>\n Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(raw).pipe(\n Effect.mapError(\n error =>\n new VoiceToolBridgeError({\n message: `Invalid tool arguments JSON: ${unknownToMessage(error)}`\n })\n )\n )\n\nconst stringifyToolOutput = (value: unknown) =>\n Schema.encodeUnknownEffect(Schema.UnknownFromJsonString)(value).pipe(\n Effect.mapError(\n error =>\n new VoiceToolBridgeError({\n message: `Could not serialize tool output: ${unknownToMessage(error)}`\n })\n )\n )\n\nconst truncateVoiceToolResult = (value: string) => {\n if (value.length <= maxVoiceToolResultCharacters) {\n return value\n }\n\n return `${value.slice(0, maxVoiceToolResultCharacters)}\\n\\n[truncated for voice; summarize from available excerpt]`\n}\n\nconst contentToSerializable = (content: Content): unknown =>\n typeof content === 'string' ? truncateVoiceToolResult(content) : content\n\nconst makeVoiceToolExecutionResult = (toolCallId: string, output: string) =>\n VoiceToolExecutionResult.make({ toolCallId, output })\n\nconst makeToolErrorResult = (toolCallId: string, error: ToolError | VoiceToolBridgeError) =>\n stringifyToolOutput({ error: error.message }).pipe(\n Effect.catchTag('VoiceToolBridgeError', () => Effect.succeed('{\"error\":\"Tool failed\"}')),\n Effect.map(output => makeVoiceToolExecutionResult(toolCallId, output))\n )\n\nexport const executeVoiceToolCall = (input: VoiceToolCallRequest) =>\n Effect.gen(function* () {\n const executor = yield* ToolExecutor\n const params = yield* parseToolArguments(input.arguments)\n const result = yield* executor.execute(\n ToolCall.make({\n id: input.callId,\n name: input.name,\n params\n })\n )\n const output = yield* stringifyToolOutput({ result: contentToSerializable(result.content) })\n\n return makeVoiceToolExecutionResult(input.callId, output)\n }).pipe(\n Effect.catchTag('ToolError', error => makeToolErrorResult(input.callId, error)),\n Effect.catchTag('VoiceToolBridgeError', error => makeToolErrorResult(input.callId, error))\n )\n"],"mappings":";;;;;AAKA,MAAM,wBAAwB,OAAO,QAAQ,KAAK,OAAO,MAAM,OAAO,WAAW,CAAC,CAAC;AACnF,MAAM,+BAA+B;AAErC,IAAa,uBAAb,cAA0C,OAAO,MAC/C,sBACF,EAAE;CACA,QAAQ;CACR,MAAM;CACN,WAAW,OAAO;AACpB,CAAC,EAAE,CAAC;AAEJ,IAAa,2BAAb,cAA8C,OAAO,MACnD,0BACF,EAAE;CACA,YAAY,OAAO;CACnB,QAAQ,OAAO;AACjB,CAAC,EAAE,CAAC;AAEJ,IAAa,uBAAb,cAA0C,OAAO,iBAAuC,EACtF,wBACA,EACE,SAAS,OAAO,OAClB,CACF,EAAE,CAAC;AAEH,MAAM,oBAAoB,UACxB,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAEvD,MAAM,sBAAsB,QAC1B,OAAO,oBAAoB,OAAO,qBAAqB,EAAE,GAAG,EAAE,KAC5D,OAAO,UACL,UACE,IAAI,qBAAqB,EACvB,SAAS,gCAAgC,iBAAiB,KAAK,IACjE,CAAC,CACL,CACF;AAEF,MAAM,uBAAuB,UAC3B,OAAO,oBAAoB,OAAO,qBAAqB,EAAE,KAAK,EAAE,KAC9D,OAAO,UACL,UACE,IAAI,qBAAqB,EACvB,SAAS,oCAAoC,iBAAiB,KAAK,IACrE,CAAC,CACL,CACF;AAEF,MAAM,2BAA2B,UAAkB;CACjD,IAAI,MAAM,UAAU,8BAClB,OAAO;CAGT,OAAO,GAAG,MAAM,MAAM,GAAG,4BAA4B,EAAE;AACzD;AAEA,MAAM,yBAAyB,YAC7B,OAAO,YAAY,WAAW,wBAAwB,OAAO,IAAI;AAEnE,MAAM,gCAAgC,YAAoB,WACxD,yBAAyB,KAAK;CAAE;CAAY;AAAO,CAAC;AAEtD,MAAM,uBAAuB,YAAoB,UAC/C,oBAAoB,EAAE,OAAO,MAAM,QAAQ,CAAC,EAAE,KAC5C,OAAO,SAAS,8BAA8B,OAAO,QAAQ,6BAAyB,CAAC,GACvF,OAAO,KAAI,WAAU,6BAA6B,YAAY,MAAM,CAAC,CACvE;AAEF,MAAa,wBAAwB,UACnC,OAAO,IAAI,aAAa;CACtB,MAAM,WAAW,OAAO;CACxB,MAAM,SAAS,OAAO,mBAAmB,MAAM,SAAS;CAQxD,MAAM,SAAS,OAAO,oBAAoB,EAAE,QAAQ,uBAAsB,OAPpD,SAAS,QAC7B,SAAS,KAAK;EACZ,IAAI,MAAM;EACV,MAAM,MAAM;EACZ;CACF,CAAC,CACH,GACiF,OAAO,EAAE,CAAC;CAE3F,OAAO,6BAA6B,MAAM,QAAQ,MAAM;AAC1D,CAAC,EAAE,KACD,OAAO,SAAS,cAAa,UAAS,oBAAoB,MAAM,QAAQ,KAAK,CAAC,GAC9E,OAAO,SAAS,yBAAwB,UAAS,oBAAoB,MAAM,QAAQ,KAAK,CAAC,CAC3F"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yolk-sdk/voice-runtime",
|
|
3
|
+
"version": "0.0.1-canary.0",
|
|
4
|
+
"description": "Provider-neutral voice tool-call bridge for Yolk agents.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/magoz/yolk-sdk.git",
|
|
11
|
+
"directory": "packages/voice-runtime"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/magoz/yolk-sdk/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/magoz/yolk-sdk#readme",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"voice",
|
|
19
|
+
"realtime",
|
|
20
|
+
"agents",
|
|
21
|
+
"tools",
|
|
22
|
+
"effect"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.mts",
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"src/**/*.ts",
|
|
37
|
+
"!src/**/*.test.ts",
|
|
38
|
+
"!src/**/*.test.tsx",
|
|
39
|
+
"dist/**/*",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"effect": "4.0.0-beta.65",
|
|
48
|
+
"@yolk-sdk/agent": "^0.0.1-canary.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"check": "tsc -p tsconfig.json --noEmit",
|
|
53
|
+
"test": "vitest run --passWithNoTests",
|
|
54
|
+
"test:run": "vitest run --passWithNoTests"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Effect } from 'effect'
|
|
2
|
+
import * as Schema from 'effect/Schema'
|
|
3
|
+
import { ToolExecutor, type ToolError } from '@yolk-sdk/agent/loop'
|
|
4
|
+
import { ToolCall, type Content } from '@yolk-sdk/agent/protocol'
|
|
5
|
+
|
|
6
|
+
const NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))
|
|
7
|
+
const maxVoiceToolResultCharacters = 6000
|
|
8
|
+
|
|
9
|
+
export class VoiceToolCallRequest extends Schema.Class<VoiceToolCallRequest>(
|
|
10
|
+
'VoiceToolCallRequest'
|
|
11
|
+
)({
|
|
12
|
+
callId: NonEmptyTrimmedString,
|
|
13
|
+
name: NonEmptyTrimmedString,
|
|
14
|
+
arguments: Schema.String
|
|
15
|
+
}) {}
|
|
16
|
+
|
|
17
|
+
export class VoiceToolExecutionResult extends Schema.Class<VoiceToolExecutionResult>(
|
|
18
|
+
'VoiceToolExecutionResult'
|
|
19
|
+
)({
|
|
20
|
+
toolCallId: Schema.String,
|
|
21
|
+
output: Schema.String
|
|
22
|
+
}) {}
|
|
23
|
+
|
|
24
|
+
export class VoiceToolBridgeError extends Schema.TaggedErrorClass<VoiceToolBridgeError>()(
|
|
25
|
+
'VoiceToolBridgeError',
|
|
26
|
+
{
|
|
27
|
+
message: Schema.String
|
|
28
|
+
}
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
const unknownToMessage = (error: unknown) =>
|
|
32
|
+
error instanceof Error ? error.message : String(error)
|
|
33
|
+
|
|
34
|
+
const parseToolArguments = (raw: string) =>
|
|
35
|
+
Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(raw).pipe(
|
|
36
|
+
Effect.mapError(
|
|
37
|
+
error =>
|
|
38
|
+
new VoiceToolBridgeError({
|
|
39
|
+
message: `Invalid tool arguments JSON: ${unknownToMessage(error)}`
|
|
40
|
+
})
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
const stringifyToolOutput = (value: unknown) =>
|
|
45
|
+
Schema.encodeUnknownEffect(Schema.UnknownFromJsonString)(value).pipe(
|
|
46
|
+
Effect.mapError(
|
|
47
|
+
error =>
|
|
48
|
+
new VoiceToolBridgeError({
|
|
49
|
+
message: `Could not serialize tool output: ${unknownToMessage(error)}`
|
|
50
|
+
})
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
const truncateVoiceToolResult = (value: string) => {
|
|
55
|
+
if (value.length <= maxVoiceToolResultCharacters) {
|
|
56
|
+
return value
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return `${value.slice(0, maxVoiceToolResultCharacters)}\n\n[truncated for voice; summarize from available excerpt]`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const contentToSerializable = (content: Content): unknown =>
|
|
63
|
+
typeof content === 'string' ? truncateVoiceToolResult(content) : content
|
|
64
|
+
|
|
65
|
+
const makeVoiceToolExecutionResult = (toolCallId: string, output: string) =>
|
|
66
|
+
VoiceToolExecutionResult.make({ toolCallId, output })
|
|
67
|
+
|
|
68
|
+
const makeToolErrorResult = (toolCallId: string, error: ToolError | VoiceToolBridgeError) =>
|
|
69
|
+
stringifyToolOutput({ error: error.message }).pipe(
|
|
70
|
+
Effect.catchTag('VoiceToolBridgeError', () => Effect.succeed('{"error":"Tool failed"}')),
|
|
71
|
+
Effect.map(output => makeVoiceToolExecutionResult(toolCallId, output))
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
export const executeVoiceToolCall = (input: VoiceToolCallRequest) =>
|
|
75
|
+
Effect.gen(function* () {
|
|
76
|
+
const executor = yield* ToolExecutor
|
|
77
|
+
const params = yield* parseToolArguments(input.arguments)
|
|
78
|
+
const result = yield* executor.execute(
|
|
79
|
+
ToolCall.make({
|
|
80
|
+
id: input.callId,
|
|
81
|
+
name: input.name,
|
|
82
|
+
params
|
|
83
|
+
})
|
|
84
|
+
)
|
|
85
|
+
const output = yield* stringifyToolOutput({ result: contentToSerializable(result.content) })
|
|
86
|
+
|
|
87
|
+
return makeVoiceToolExecutionResult(input.callId, output)
|
|
88
|
+
}).pipe(
|
|
89
|
+
Effect.catchTag('ToolError', error => makeToolErrorResult(input.callId, error)),
|
|
90
|
+
Effect.catchTag('VoiceToolBridgeError', error => makeToolErrorResult(input.callId, error))
|
|
91
|
+
)
|