@livekit/agents 1.0.46 → 1.0.48
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/dist/beta/index.cjs +29 -0
- package/dist/beta/index.cjs.map +1 -0
- package/dist/beta/index.d.cts +2 -0
- package/dist/beta/index.d.ts +2 -0
- package/dist/beta/index.d.ts.map +1 -0
- package/dist/beta/index.js +7 -0
- package/dist/beta/index.js.map +1 -0
- package/dist/beta/workflows/index.cjs +29 -0
- package/dist/beta/workflows/index.cjs.map +1 -0
- package/dist/beta/workflows/index.d.cts +2 -0
- package/dist/beta/workflows/index.d.ts +2 -0
- package/dist/beta/workflows/index.d.ts.map +1 -0
- package/dist/beta/workflows/index.js +7 -0
- package/dist/beta/workflows/index.js.map +1 -0
- package/dist/beta/workflows/task_group.cjs +162 -0
- package/dist/beta/workflows/task_group.cjs.map +1 -0
- package/dist/beta/workflows/task_group.d.cts +32 -0
- package/dist/beta/workflows/task_group.d.ts +32 -0
- package/dist/beta/workflows/task_group.d.ts.map +1 -0
- package/dist/beta/workflows/task_group.js +138 -0
- package/dist/beta/workflows/task_group.js.map +1 -0
- package/dist/cli.cjs +14 -20
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -20
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/inference/api_protos.d.cts +59 -59
- package/dist/inference/api_protos.d.ts +59 -59
- package/dist/ipc/job_proc_lazy_main.cjs +14 -5
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +14 -5
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/llm/chat_context.cjs +108 -1
- package/dist/llm/chat_context.cjs.map +1 -1
- package/dist/llm/chat_context.d.cts +14 -1
- package/dist/llm/chat_context.d.ts +14 -1
- package/dist/llm/chat_context.d.ts.map +1 -1
- package/dist/llm/chat_context.js +108 -1
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/chat_context.test.cjs +43 -0
- package/dist/llm/chat_context.test.cjs.map +1 -1
- package/dist/llm/chat_context.test.js +43 -0
- package/dist/llm/chat_context.test.js.map +1 -1
- package/dist/llm/index.cjs +2 -0
- package/dist/llm/index.cjs.map +1 -1
- package/dist/llm/index.d.cts +1 -1
- package/dist/llm/index.d.ts +1 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +3 -1
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/provider_format/index.cjs +2 -0
- package/dist/llm/provider_format/index.cjs.map +1 -1
- package/dist/llm/provider_format/index.d.cts +2 -2
- package/dist/llm/provider_format/index.d.ts +2 -2
- package/dist/llm/provider_format/index.d.ts.map +1 -1
- package/dist/llm/provider_format/index.js +6 -1
- package/dist/llm/provider_format/index.js.map +1 -1
- package/dist/llm/provider_format/openai.cjs +82 -2
- package/dist/llm/provider_format/openai.cjs.map +1 -1
- package/dist/llm/provider_format/openai.d.cts +1 -0
- package/dist/llm/provider_format/openai.d.ts +1 -0
- package/dist/llm/provider_format/openai.d.ts.map +1 -1
- package/dist/llm/provider_format/openai.js +80 -1
- package/dist/llm/provider_format/openai.js.map +1 -1
- package/dist/llm/provider_format/openai.test.cjs +326 -0
- package/dist/llm/provider_format/openai.test.cjs.map +1 -1
- package/dist/llm/provider_format/openai.test.js +327 -1
- package/dist/llm/provider_format/openai.test.js.map +1 -1
- package/dist/llm/provider_format/utils.cjs +4 -3
- package/dist/llm/provider_format/utils.cjs.map +1 -1
- package/dist/llm/provider_format/utils.d.ts.map +1 -1
- package/dist/llm/provider_format/utils.js +4 -3
- package/dist/llm/provider_format/utils.js.map +1 -1
- package/dist/llm/realtime.cjs.map +1 -1
- package/dist/llm/realtime.d.cts +1 -0
- package/dist/llm/realtime.d.ts +1 -0
- package/dist/llm/realtime.d.ts.map +1 -1
- package/dist/llm/realtime.js.map +1 -1
- package/dist/llm/tool_context.cjs +7 -0
- package/dist/llm/tool_context.cjs.map +1 -1
- package/dist/llm/tool_context.d.cts +10 -2
- package/dist/llm/tool_context.d.ts +10 -2
- package/dist/llm/tool_context.d.ts.map +1 -1
- package/dist/llm/tool_context.js +6 -0
- package/dist/llm/tool_context.js.map +1 -1
- package/dist/log.cjs +5 -2
- package/dist/log.cjs.map +1 -1
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +5 -2
- package/dist/log.js.map +1 -1
- package/dist/stream/deferred_stream.cjs +15 -6
- package/dist/stream/deferred_stream.cjs.map +1 -1
- package/dist/stream/deferred_stream.d.ts.map +1 -1
- package/dist/stream/deferred_stream.js +15 -6
- package/dist/stream/deferred_stream.js.map +1 -1
- package/dist/utils.cjs +32 -2
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +7 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +32 -2
- package/dist/utils.js.map +1 -1
- package/dist/utils.test.cjs +71 -0
- package/dist/utils.test.cjs.map +1 -1
- package/dist/utils.test.js +71 -0
- package/dist/utils.test.js.map +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.cjs.map +1 -1
- package/dist/version.d.cts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/voice/agent.cjs +153 -12
- package/dist/voice/agent.cjs.map +1 -1
- package/dist/voice/agent.d.cts +30 -4
- package/dist/voice/agent.d.ts +30 -4
- package/dist/voice/agent.d.ts.map +1 -1
- package/dist/voice/agent.js +149 -11
- package/dist/voice/agent.js.map +1 -1
- package/dist/voice/agent.test.cjs +120 -0
- package/dist/voice/agent.test.cjs.map +1 -1
- package/dist/voice/agent.test.js +122 -2
- package/dist/voice/agent.test.js.map +1 -1
- package/dist/voice/agent_activity.cjs +406 -298
- package/dist/voice/agent_activity.cjs.map +1 -1
- package/dist/voice/agent_activity.d.cts +41 -7
- package/dist/voice/agent_activity.d.ts +41 -7
- package/dist/voice/agent_activity.d.ts.map +1 -1
- package/dist/voice/agent_activity.js +407 -294
- package/dist/voice/agent_activity.js.map +1 -1
- package/dist/voice/agent_session.cjs +140 -40
- package/dist/voice/agent_session.cjs.map +1 -1
- package/dist/voice/agent_session.d.cts +19 -7
- package/dist/voice/agent_session.d.ts +19 -7
- package/dist/voice/agent_session.d.ts.map +1 -1
- package/dist/voice/agent_session.js +137 -37
- package/dist/voice/agent_session.js.map +1 -1
- package/dist/voice/audio_recognition.cjs +4 -0
- package/dist/voice/audio_recognition.cjs.map +1 -1
- package/dist/voice/audio_recognition.d.ts.map +1 -1
- package/dist/voice/audio_recognition.js +4 -0
- package/dist/voice/audio_recognition.js.map +1 -1
- package/dist/voice/generation.cjs +39 -19
- package/dist/voice/generation.cjs.map +1 -1
- package/dist/voice/generation.d.ts.map +1 -1
- package/dist/voice/generation.js +44 -20
- package/dist/voice/generation.js.map +1 -1
- package/dist/voice/index.cjs +2 -0
- package/dist/voice/index.cjs.map +1 -1
- package/dist/voice/index.d.cts +1 -1
- package/dist/voice/index.d.ts +1 -1
- package/dist/voice/index.d.ts.map +1 -1
- package/dist/voice/index.js +2 -1
- package/dist/voice/index.js.map +1 -1
- package/dist/voice/room_io/room_io.cjs +11 -2
- package/dist/voice/room_io/room_io.cjs.map +1 -1
- package/dist/voice/room_io/room_io.d.ts.map +1 -1
- package/dist/voice/room_io/room_io.js +12 -3
- package/dist/voice/room_io/room_io.js.map +1 -1
- package/dist/voice/speech_handle.cjs +7 -1
- package/dist/voice/speech_handle.cjs.map +1 -1
- package/dist/voice/speech_handle.d.cts +2 -0
- package/dist/voice/speech_handle.d.ts +2 -0
- package/dist/voice/speech_handle.d.ts.map +1 -1
- package/dist/voice/speech_handle.js +8 -2
- package/dist/voice/speech_handle.js.map +1 -1
- package/dist/voice/testing/fake_llm.cjs +127 -0
- package/dist/voice/testing/fake_llm.cjs.map +1 -0
- package/dist/voice/testing/fake_llm.d.cts +30 -0
- package/dist/voice/testing/fake_llm.d.ts +30 -0
- package/dist/voice/testing/fake_llm.d.ts.map +1 -0
- package/dist/voice/testing/fake_llm.js +103 -0
- package/dist/voice/testing/fake_llm.js.map +1 -0
- package/dist/voice/testing/index.cjs +3 -0
- package/dist/voice/testing/index.cjs.map +1 -1
- package/dist/voice/testing/index.d.cts +1 -0
- package/dist/voice/testing/index.d.ts +1 -0
- package/dist/voice/testing/index.d.ts.map +1 -1
- package/dist/voice/testing/index.js +2 -0
- package/dist/voice/testing/index.js.map +1 -1
- package/dist/voice/testing/run_result.cjs +66 -15
- package/dist/voice/testing/run_result.cjs.map +1 -1
- package/dist/voice/testing/run_result.d.cts +14 -3
- package/dist/voice/testing/run_result.d.ts +14 -3
- package/dist/voice/testing/run_result.d.ts.map +1 -1
- package/dist/voice/testing/run_result.js +66 -15
- package/dist/voice/testing/run_result.js.map +1 -1
- package/package.json +1 -1
- package/src/beta/index.ts +9 -0
- package/src/beta/workflows/index.ts +9 -0
- package/src/beta/workflows/task_group.ts +194 -0
- package/src/cli.ts +20 -33
- package/src/index.ts +2 -1
- package/src/ipc/job_proc_lazy_main.ts +16 -5
- package/src/llm/chat_context.test.ts +48 -0
- package/src/llm/chat_context.ts +158 -0
- package/src/llm/index.ts +1 -0
- package/src/llm/provider_format/index.ts +7 -2
- package/src/llm/provider_format/openai.test.ts +385 -1
- package/src/llm/provider_format/openai.ts +103 -0
- package/src/llm/provider_format/utils.ts +6 -4
- package/src/llm/realtime.ts +1 -0
- package/src/llm/tool_context.ts +14 -0
- package/src/log.ts +5 -2
- package/src/stream/deferred_stream.ts +17 -6
- package/src/utils.test.ts +87 -0
- package/src/utils.ts +41 -2
- package/src/version.ts +1 -1
- package/src/voice/agent.test.ts +140 -2
- package/src/voice/agent.ts +200 -10
- package/src/voice/agent_activity.ts +466 -290
- package/src/voice/agent_session.ts +178 -40
- package/src/voice/audio_recognition.ts +4 -0
- package/src/voice/generation.ts +52 -23
- package/src/voice/index.ts +1 -1
- package/src/voice/room_io/room_io.ts +14 -3
- package/src/voice/speech_handle.ts +9 -2
- package/src/voice/testing/fake_llm.ts +138 -0
- package/src/voice/testing/index.ts +2 -0
- package/src/voice/testing/run_result.ts +81 -23
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n
|
|
1
|
+
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n const sameIds =\n toolCallIds.size === toolOutputIds.size &&\n [...toolCallIds].every((id) => toolOutputIds.has(id));\n if (this.toolCalls.length === this.toolOutputs.length && sameIds) {\n return;\n }\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n // For function calls, use group_id if available (for parallel function calls),\n // otherwise fall back to id-based grouping for backwards compatibility\n const groupId =\n item.type === 'function_call' && item.groupId ? item.groupId : item.id.split('/')[0]!;\n\n if (itemGroups[groupId] === undefined) {\n itemGroups[groupId] = ChatItemGroup.create();\n\n // we use insertion order to sort the groups as they are added to the context\n // simulating the OrderedDict in python\n insertionOrder[groupId] = insertionIndex;\n insertionIndex++;\n }\n itemGroups[groupId]!.add(item);\n } else if (isFunctionCallOutput) {\n toolOutputs.push(item);\n } else {\n itemGroups[item.id] = ChatItemGroup.create().add(item);\n }\n }\n\n // add tool outputs to their corresponding groups\n const callIdToGroup: Record<string, ChatItemGroup> = {};\n for (const group of Object.values(itemGroups)) {\n for (const toolCall of group.toolCalls) {\n callIdToGroup[toolCall.callId] = group;\n }\n }\n\n for (const toolOutput of toolOutputs) {\n const group = callIdToGroup[toolOutput.callId];\n if (group === undefined) {\n logger.warn(\n { callId: toolOutput.callId, toolName: toolOutput.name },\n 'function output missing the corresponding function call, ignoring',\n );\n continue;\n }\n group.add(toolOutput);\n }\n\n // validate that each group and remove invalid tool calls and tool outputs\n for (const group of Object.values(itemGroups)) {\n group.removeInvalidToolCalls();\n }\n\n // sort groups by their item id\n const orderedGroups = Object.entries(itemGroups)\n .sort((a, b) => insertionOrder[a[0]]! - insertionOrder[b[0]]!)\n .map(([, group]) => group);\n return orderedGroups;\n}\n"],"mappings":"AAGA,SAAS,WAAW;AASpB,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,IAAI;AAAA,EAEb,YAAY,QAIT;AACD,SAAK,UAAU,OAAO;AACtB,SAAK,YAAY,OAAO;AACxB,SAAK,cAAc,OAAO;AAAA,EAC5B;AAAA,EAEA,OAAO,OAAO,QAIX;AACD,UAAM,EAAE,SAAS,YAAY,CAAC,GAAG,cAAc,CAAC,EAAE,IAAI,UAAU,CAAC;AACjE,WAAO,IAAI,cAAc,EAAE,SAAS,WAAW,YAAY,CAAC;AAAA,EAC9D;AAAA,EAEA,IAAI,UAAU;AACZ,WACE,KAAK,YAAY,UAAa,KAAK,UAAU,WAAW,KAAK,KAAK,YAAY,WAAW;AAAA,EAE7F;AAAA,EAEA,IAAI,MAAgB;AAClB,QAAI,KAAK,SAAS,WAAW;AAC3B,UAAI,KAAK,SAAS;AAChB,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,WAAK,UAAU;AAAA,IACjB,WAAW,KAAK,SAAS,iBAAiB;AACxC,WAAK,UAAU,KAAK,IAAI;AAAA,IAC1B,WAAW,KAAK,SAAS,wBAAwB;AAC/C,WAAK,YAAY,KA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AAU,EAC5C,KAAK,CAAC,GAAG,MAAM,eAAe,EAAE,CAAC,CAAC,IAAK,eAAe,EAAE,CAAC,CAAC,CAAE,EAC5D,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,KAAK;AAC3B,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/llm/realtime.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { EventEmitter } from 'events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { Task } from '../utils.js';\nimport type { TimedString } from '../voice/io.js';\nimport type { ChatContext, FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport type InputSpeechStartedEvent = object;\n\nexport interface InputSpeechStoppedEvent {\n userTranscriptionEnabled: boolean;\n}\n\nexport interface MessageGeneration {\n messageId: string;\n /**\n * Text stream that may contain plain strings or TimedString objects with timestamps.\n */\n textStream: ReadableStream<string | TimedString>;\n audioStream: ReadableStream<AudioFrame>;\n modalities?: Promise<('text' | 'audio')[]>;\n}\n\nexport interface GenerationCreatedEvent {\n messageStream: ReadableStream<MessageGeneration>;\n functionStream: ReadableStream<FunctionCall>;\n userInitiated: boolean;\n /** Response ID for correlating metrics with spans */\n responseId?: string;\n}\n\nexport interface RealtimeModelError {\n type: 'realtime_model_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport interface RealtimeCapabilities {\n messageTruncation: boolean;\n turnDetection: boolean;\n userTranscription: boolean;\n autoToolReplyGeneration: boolean;\n audioOutput: boolean;\n}\n\nexport interface InputTranscriptionCompleted {\n itemId: string;\n transcript: string;\n isFinal: boolean;\n}\n\nexport interface RealtimeSessionReconnectedEvent {}\n\nexport abstract class RealtimeModel {\n private _capabilities: RealtimeCapabilities;\n\n constructor(capabilities: RealtimeCapabilities) {\n this._capabilities = capabilities;\n }\n\n get capabilities() {\n return this._capabilities;\n }\n\n /** The model name/identifier used by this realtime model */\n abstract get model(): string;\n\n abstract session(): RealtimeSession;\n\n abstract close(): Promise<void>;\n}\n\nexport abstract class RealtimeSession extends EventEmitter {\n protected _realtimeModel: RealtimeModel;\n private deferredInputStream = new DeferredReadableStream<AudioFrame>();\n private _mainTask: Task<void>;\n\n constructor(realtimeModel: RealtimeModel) {\n super();\n this._realtimeModel = realtimeModel;\n this._mainTask = Task.from((controller) => this._mainTaskImpl(controller.signal));\n }\n\n get realtimeModel() {\n return this._realtimeModel;\n }\n\n abstract get chatCtx(): ChatContext;\n\n abstract get tools(): ToolContext;\n\n abstract updateInstructions(instructions: string): Promise<void>;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;\n\n abstract updateTools(tools: ToolContext): Promise<void>;\n\n abstract updateOptions(options: { toolChoice?: ToolChoice | null }): void;\n\n abstract pushAudio(frame: AudioFrame): void;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;\n\n /**\n * Commit the input audio buffer to the server\n */\n abstract commitAudio(): Promise<void>;\n\n /**\n * Clear the input audio buffer to the server\n */\n abstract clearAudio(): Promise<void>;\n\n /**\n * Cancel the current generation (do nothing if no generation is in progress)\n */\n abstract interrupt(): Promise<void>;\n\n /**\n * Truncate the message at the given audio end time\n */\n abstract truncate(options: {\n messageId: string;\n audioEndMs: number;\n modalities?: ('text' | 'audio')[];\n audioTranscript?: string;\n }): Promise<void>;\n\n async close(): Promise<void> {\n this._mainTask.cancel();\n }\n\n /**\n * Notifies the model that user activity has started\n */\n startUserActivity(): void {\n return;\n }\n\n private async _mainTaskImpl(signal: AbortSignal): Promise<void> {\n const reader = this.deferredInputStream.stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done || signal.aborted) {\n break;\n }\n this.pushAudio(value);\n }\n }\n\n setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void {\n this.deferredInputStream.setSource(audioStream);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAA6B;AAE7B,6BAAuC;AACvC,mBAAqB;
|
|
1
|
+
{"version":3,"sources":["../../src/llm/realtime.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { EventEmitter } from 'events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { Task } from '../utils.js';\nimport type { TimedString } from '../voice/io.js';\nimport type { ChatContext, FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport type InputSpeechStartedEvent = object;\n\nexport interface InputSpeechStoppedEvent {\n userTranscriptionEnabled: boolean;\n}\n\nexport interface MessageGeneration {\n messageId: string;\n /**\n * Text stream that may contain plain strings or TimedString objects with timestamps.\n */\n textStream: ReadableStream<string | TimedString>;\n audioStream: ReadableStream<AudioFrame>;\n modalities?: Promise<('text' | 'audio')[]>;\n}\n\nexport interface GenerationCreatedEvent {\n messageStream: ReadableStream<MessageGeneration>;\n functionStream: ReadableStream<FunctionCall>;\n userInitiated: boolean;\n /** Response ID for correlating metrics with spans */\n responseId?: string;\n}\n\nexport interface RealtimeModelError {\n type: 'realtime_model_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport interface RealtimeCapabilities {\n messageTruncation: boolean;\n turnDetection: boolean;\n userTranscription: boolean;\n autoToolReplyGeneration: boolean;\n audioOutput: boolean;\n manualFunctionCalls: boolean;\n}\n\nexport interface InputTranscriptionCompleted {\n itemId: string;\n transcript: string;\n isFinal: boolean;\n}\n\nexport interface RealtimeSessionReconnectedEvent {}\n\nexport abstract class RealtimeModel {\n private _capabilities: RealtimeCapabilities;\n\n constructor(capabilities: RealtimeCapabilities) {\n this._capabilities = capabilities;\n }\n\n get capabilities() {\n return this._capabilities;\n }\n\n /** The model name/identifier used by this realtime model */\n abstract get model(): string;\n\n abstract session(): RealtimeSession;\n\n abstract close(): Promise<void>;\n}\n\nexport abstract class RealtimeSession extends EventEmitter {\n protected _realtimeModel: RealtimeModel;\n private deferredInputStream = new DeferredReadableStream<AudioFrame>();\n private _mainTask: Task<void>;\n\n constructor(realtimeModel: RealtimeModel) {\n super();\n this._realtimeModel = realtimeModel;\n this._mainTask = Task.from((controller) => this._mainTaskImpl(controller.signal));\n }\n\n get realtimeModel() {\n return this._realtimeModel;\n }\n\n abstract get chatCtx(): ChatContext;\n\n abstract get tools(): ToolContext;\n\n abstract updateInstructions(instructions: string): Promise<void>;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;\n\n abstract updateTools(tools: ToolContext): Promise<void>;\n\n abstract updateOptions(options: { toolChoice?: ToolChoice | null }): void;\n\n abstract pushAudio(frame: AudioFrame): void;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;\n\n /**\n * Commit the input audio buffer to the server\n */\n abstract commitAudio(): Promise<void>;\n\n /**\n * Clear the input audio buffer to the server\n */\n abstract clearAudio(): Promise<void>;\n\n /**\n * Cancel the current generation (do nothing if no generation is in progress)\n */\n abstract interrupt(): Promise<void>;\n\n /**\n * Truncate the message at the given audio end time\n */\n abstract truncate(options: {\n messageId: string;\n audioEndMs: number;\n modalities?: ('text' | 'audio')[];\n audioTranscript?: string;\n }): Promise<void>;\n\n async close(): Promise<void> {\n this._mainTask.cancel();\n }\n\n /**\n * Notifies the model that user activity has started\n */\n startUserActivity(): void {\n return;\n }\n\n private async _mainTaskImpl(signal: AbortSignal): Promise<void> {\n const reader = this.deferredInputStream.stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done || signal.aborted) {\n break;\n }\n this.pushAudio(value);\n }\n }\n\n setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void {\n this.deferredInputStream.setSource(audioStream);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAA6B;AAE7B,6BAAuC;AACvC,mBAAqB;AAsDd,MAAe,cAAc;AAAA,EAC1B;AAAA,EAER,YAAY,cAAoC;AAC9C,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,IAAI,eAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAQF;AAEO,MAAe,wBAAwB,2BAAa;AAAA,EAC/C;AAAA,EACF,sBAAsB,IAAI,8CAAmC;AAAA,EAC7D;AAAA,EAER,YAAY,eAA8B;AACxC,UAAM;AACN,SAAK,iBAAiB;AACtB,SAAK,YAAY,kBAAK,KAAK,CAAC,eAAe,KAAK,cAAc,WAAW,MAAM,CAAC;AAAA,EAClF;AAAA,EAEA,IAAI,gBAAgB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAiDA,MAAM,QAAuB;AAC3B,SAAK,UAAU,OAAO;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,oBAA0B;AACxB;AAAA,EACF;AAAA,EAEA,MAAc,cAAc,QAAoC;AAC9D,UAAM,SAAS,KAAK,oBAAoB,OAAO,UAAU;AACzD,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AACA,WAAK,UAAU,KAAK;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,oBAAoB,aAA+C;AACjE,SAAK,oBAAoB,UAAU,WAAW;AAAA,EAChD;AACF;","names":[]}
|
package/dist/llm/realtime.d.cts
CHANGED
package/dist/llm/realtime.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"realtime.d.ts","sour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WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAEvD,QAAQ,CAAC,aAAa,CAAC,OAAO,EAAE;QAAE,UAAU,CAAC,EAAE,UAAU,GAAG,IAAI,CAAA;KAAE,GAAG,IAAI;IAEzE,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAE3C;;OAEG;IACH,QAAQ,CAAC,aAAa,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAE9E;;OAEG;IACH,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAErC;;OAEG;IACH,QAAQ,CAAC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAEnC;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE;QACzB,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,UAAU,CAAC,EAAE,CAAC,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC;QAClC,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,GAAG,OAAO,CAAC,IAAI,CAAC;IAEX,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACH,iBAAiB,IAAI,IAAI;YAIX,aAAa;IAW3B,mBAAmB,CAAC,WAAW,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI;CAGnE"}
|
package/dist/llm/realtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/llm/realtime.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { EventEmitter } from 'events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { Task } from '../utils.js';\nimport type { TimedString } from '../voice/io.js';\nimport type { ChatContext, FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport type InputSpeechStartedEvent = object;\n\nexport interface InputSpeechStoppedEvent {\n userTranscriptionEnabled: boolean;\n}\n\nexport interface MessageGeneration {\n messageId: string;\n /**\n * Text stream that may contain plain strings or TimedString objects with timestamps.\n */\n textStream: ReadableStream<string | TimedString>;\n audioStream: ReadableStream<AudioFrame>;\n modalities?: Promise<('text' | 'audio')[]>;\n}\n\nexport interface GenerationCreatedEvent {\n messageStream: ReadableStream<MessageGeneration>;\n functionStream: ReadableStream<FunctionCall>;\n userInitiated: boolean;\n /** Response ID for correlating metrics with spans */\n responseId?: string;\n}\n\nexport interface RealtimeModelError {\n type: 'realtime_model_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport interface RealtimeCapabilities {\n messageTruncation: boolean;\n turnDetection: boolean;\n userTranscription: boolean;\n autoToolReplyGeneration: boolean;\n audioOutput: boolean;\n}\n\nexport interface InputTranscriptionCompleted {\n itemId: string;\n transcript: string;\n isFinal: boolean;\n}\n\nexport interface RealtimeSessionReconnectedEvent {}\n\nexport abstract class RealtimeModel {\n private _capabilities: RealtimeCapabilities;\n\n constructor(capabilities: RealtimeCapabilities) {\n this._capabilities = capabilities;\n }\n\n get capabilities() {\n return this._capabilities;\n }\n\n /** The model name/identifier used by this realtime model */\n abstract get model(): string;\n\n abstract session(): RealtimeSession;\n\n abstract close(): Promise<void>;\n}\n\nexport abstract class RealtimeSession extends EventEmitter {\n protected _realtimeModel: RealtimeModel;\n private deferredInputStream = new DeferredReadableStream<AudioFrame>();\n private _mainTask: Task<void>;\n\n constructor(realtimeModel: RealtimeModel) {\n super();\n this._realtimeModel = realtimeModel;\n this._mainTask = Task.from((controller) => this._mainTaskImpl(controller.signal));\n }\n\n get realtimeModel() {\n return this._realtimeModel;\n }\n\n abstract get chatCtx(): ChatContext;\n\n abstract get tools(): ToolContext;\n\n abstract updateInstructions(instructions: string): Promise<void>;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;\n\n abstract updateTools(tools: ToolContext): Promise<void>;\n\n abstract updateOptions(options: { toolChoice?: ToolChoice | null }): void;\n\n abstract pushAudio(frame: AudioFrame): void;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;\n\n /**\n * Commit the input audio buffer to the server\n */\n abstract commitAudio(): Promise<void>;\n\n /**\n * Clear the input audio buffer to the server\n */\n abstract clearAudio(): Promise<void>;\n\n /**\n * Cancel the current generation (do nothing if no generation is in progress)\n */\n abstract interrupt(): Promise<void>;\n\n /**\n * Truncate the message at the given audio end time\n */\n abstract truncate(options: {\n messageId: string;\n audioEndMs: number;\n modalities?: ('text' | 'audio')[];\n audioTranscript?: string;\n }): Promise<void>;\n\n async close(): Promise<void> {\n this._mainTask.cancel();\n }\n\n /**\n * Notifies the model that user activity has started\n */\n startUserActivity(): void {\n return;\n }\n\n private async _mainTaskImpl(signal: AbortSignal): Promise<void> {\n const reader = this.deferredInputStream.stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done || signal.aborted) {\n break;\n }\n this.pushAudio(value);\n }\n }\n\n setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void {\n this.deferredInputStream.setSource(audioStream);\n }\n}\n"],"mappings":"AAIA,SAAS,oBAAoB;AAE7B,SAAS,8BAA8B;AACvC,SAAS,YAAY;
|
|
1
|
+
{"version":3,"sources":["../../src/llm/realtime.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { EventEmitter } from 'events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { Task } from '../utils.js';\nimport type { TimedString } from '../voice/io.js';\nimport type { ChatContext, FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport type InputSpeechStartedEvent = object;\n\nexport interface InputSpeechStoppedEvent {\n userTranscriptionEnabled: boolean;\n}\n\nexport interface MessageGeneration {\n messageId: string;\n /**\n * Text stream that may contain plain strings or TimedString objects with timestamps.\n */\n textStream: ReadableStream<string | TimedString>;\n audioStream: ReadableStream<AudioFrame>;\n modalities?: Promise<('text' | 'audio')[]>;\n}\n\nexport interface GenerationCreatedEvent {\n messageStream: ReadableStream<MessageGeneration>;\n functionStream: ReadableStream<FunctionCall>;\n userInitiated: boolean;\n /** Response ID for correlating metrics with spans */\n responseId?: string;\n}\n\nexport interface RealtimeModelError {\n type: 'realtime_model_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport interface RealtimeCapabilities {\n messageTruncation: boolean;\n turnDetection: boolean;\n userTranscription: boolean;\n autoToolReplyGeneration: boolean;\n audioOutput: boolean;\n manualFunctionCalls: boolean;\n}\n\nexport interface InputTranscriptionCompleted {\n itemId: string;\n transcript: string;\n isFinal: boolean;\n}\n\nexport interface RealtimeSessionReconnectedEvent {}\n\nexport abstract class RealtimeModel {\n private _capabilities: RealtimeCapabilities;\n\n constructor(capabilities: RealtimeCapabilities) {\n this._capabilities = capabilities;\n }\n\n get capabilities() {\n return this._capabilities;\n }\n\n /** The model name/identifier used by this realtime model */\n abstract get model(): string;\n\n abstract session(): RealtimeSession;\n\n abstract close(): Promise<void>;\n}\n\nexport abstract class RealtimeSession extends EventEmitter {\n protected _realtimeModel: RealtimeModel;\n private deferredInputStream = new DeferredReadableStream<AudioFrame>();\n private _mainTask: Task<void>;\n\n constructor(realtimeModel: RealtimeModel) {\n super();\n this._realtimeModel = realtimeModel;\n this._mainTask = Task.from((controller) => this._mainTaskImpl(controller.signal));\n }\n\n get realtimeModel() {\n return this._realtimeModel;\n }\n\n abstract get chatCtx(): ChatContext;\n\n abstract get tools(): ToolContext;\n\n abstract updateInstructions(instructions: string): Promise<void>;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;\n\n abstract updateTools(tools: ToolContext): Promise<void>;\n\n abstract updateOptions(options: { toolChoice?: ToolChoice | null }): void;\n\n abstract pushAudio(frame: AudioFrame): void;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;\n\n /**\n * Commit the input audio buffer to the server\n */\n abstract commitAudio(): Promise<void>;\n\n /**\n * Clear the input audio buffer to the server\n */\n abstract clearAudio(): Promise<void>;\n\n /**\n * Cancel the current generation (do nothing if no generation is in progress)\n */\n abstract interrupt(): Promise<void>;\n\n /**\n * Truncate the message at the given audio end time\n */\n abstract truncate(options: {\n messageId: string;\n audioEndMs: number;\n modalities?: ('text' | 'audio')[];\n audioTranscript?: string;\n }): Promise<void>;\n\n async close(): Promise<void> {\n this._mainTask.cancel();\n }\n\n /**\n * Notifies the model that user activity has started\n */\n startUserActivity(): void {\n return;\n }\n\n private async _mainTaskImpl(signal: AbortSignal): Promise<void> {\n const reader = this.deferredInputStream.stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done || signal.aborted) {\n break;\n }\n this.pushAudio(value);\n }\n }\n\n setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void {\n this.deferredInputStream.setSource(audioStream);\n }\n}\n"],"mappings":"AAIA,SAAS,oBAAoB;AAE7B,SAAS,8BAA8B;AACvC,SAAS,YAAY;AAsDd,MAAe,cAAc;AAAA,EAC1B;AAAA,EAER,YAAY,cAAoC;AAC9C,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,IAAI,eAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAQF;AAEO,MAAe,wBAAwB,aAAa;AAAA,EAC/C;AAAA,EACF,sBAAsB,IAAI,uBAAmC;AAAA,EAC7D;AAAA,EAER,YAAY,eAA8B;AACxC,UAAM;AACN,SAAK,iBAAiB;AACtB,SAAK,YAAY,KAAK,KAAK,CAAC,eAAe,KAAK,cAAc,WAAW,MAAM,CAAC;AAAA,EAClF;AAAA,EAEA,IAAI,gBAAgB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAiDA,MAAM,QAAuB;AAC3B,SAAK,UAAU,OAAO;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,oBAA0B;AACxB;AAAA,EACF;AAAA,EAEA,MAAc,cAAc,QAAoC;AAC9D,UAAM,SAAS,KAAK,oBAAoB,OAAO,UAAU;AACzD,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AACA,WAAK,UAAU,KAAK;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,oBAAoB,aAA+C;AACjE,SAAK,oBAAoB,UAAU,WAAW;AAAA,EAChD;AACF;","names":[]}
|
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var tool_context_exports = {};
|
|
20
20
|
__export(tool_context_exports, {
|
|
21
21
|
ToolError: () => ToolError,
|
|
22
|
+
ToolFlag: () => ToolFlag,
|
|
22
23
|
handoff: () => handoff,
|
|
23
24
|
isAgentHandoff: () => isAgentHandoff,
|
|
24
25
|
isFunctionTool: () => isFunctionTool,
|
|
@@ -45,6 +46,10 @@ class ToolError extends Error {
|
|
|
45
46
|
});
|
|
46
47
|
}
|
|
47
48
|
}
|
|
49
|
+
const ToolFlag = {
|
|
50
|
+
NONE: 0,
|
|
51
|
+
IGNORE_ON_ENTER: 1 << 0
|
|
52
|
+
};
|
|
48
53
|
function handoff(options) {
|
|
49
54
|
return {
|
|
50
55
|
agent: options.agent,
|
|
@@ -102,6 +107,7 @@ function tool(tool2) {
|
|
|
102
107
|
description: tool2.description,
|
|
103
108
|
parameters,
|
|
104
109
|
execute: tool2.execute,
|
|
110
|
+
flags: tool2.flags ?? ToolFlag.NONE,
|
|
105
111
|
[TOOL_SYMBOL]: true,
|
|
106
112
|
[FUNCTION_TOOL_SYMBOL]: true
|
|
107
113
|
};
|
|
@@ -139,6 +145,7 @@ function isAgentHandoff(handoff2) {
|
|
|
139
145
|
// Annotate the CommonJS export names for ESM import in node:
|
|
140
146
|
0 && (module.exports = {
|
|
141
147
|
ToolError,
|
|
148
|
+
ToolFlag,
|
|
142
149
|
handoff,
|
|
143
150
|
isAgentHandoff,
|
|
144
151
|
isFunctionTool,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/llm/tool_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { Agent } from '../voice/agent.js';\nimport type { RunContext, UnknownUserData } from '../voice/run_context.js';\nimport { isZodObjectSchema, isZodSchema } from './zod-utils.js';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\nconst TOOL_SYMBOL = Symbol('tool');\nconst FUNCTION_TOOL_SYMBOL = Symbol('function_tool');\nconst PROVIDER_DEFINED_TOOL_SYMBOL = Symbol('provider_defined_tool');\nconst TOOL_ERROR_SYMBOL = Symbol('tool_error');\nconst HANDOFF_SYMBOL = Symbol('handoff');\n\nexport type JSONValue = null | string | number | boolean | JSONObject | JSONArray;\n\nexport type JSONArray = JSONValue[];\n\nexport type JSONObject = {\n [key: string]: JSONValue;\n};\n\n// Supports both Zod v3 and v4 schemas, as well as raw JSON schema\n// Adapted from Vercel AI SDK's FlexibleSchema approach\n// Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L67-L70\n//\n// Vercel uses StandardSchemaV1 from @standard-schema/spec package.\n// We use a simpler approach by directly checking for schema properties:\n// - Zod v3: Has `_output` property\n// - Zod v4: Implements Standard Schema spec with `~standard` property\n// - JSON Schema: Plain object fallback\nexport type ToolInputSchema<T = JSONObject> =\n | {\n // Zod v3 schema - has _output property for type inference\n _output: T;\n }\n | {\n // Zod v4 schema (Standard Schema) - has ~standard property\n '~standard': {\n types?: { output: T };\n };\n }\n | JSONSchema7;\n\n/**\n * Infer the output type from a ToolInputSchema.\n * Adapted from Vercel AI SDK's InferSchema type.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L72-L79\n */\nexport type InferToolInput<T> = T extends { _output: infer O }\n ? O\n : T extends { '~standard': { types?: { output: infer O } } }\n ? O\n : any; // eslint-disable-line @typescript-eslint/no-explicit-any -- Fallback type for JSON Schema objects without type inference\n\nexport type ToolType = 'function' | 'provider-defined';\n\nexport type ToolChoice =\n | 'auto'\n | 'none'\n | 'required'\n | {\n type: 'function';\n function: {\n name: string;\n };\n };\n\nexport class ToolError extends Error {\n constructor(message: string) {\n super(message);\n\n Object.defineProperty(this, TOOL_ERROR_SYMBOL, {\n value: true,\n });\n }\n}\n\nexport interface AgentHandoff {\n /**\n * The agent to handoff to.\n */\n agent: Agent;\n\n /**\n * The return value of the tool.\n */\n returns?: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n [HANDOFF_SYMBOL]: true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handoff(options: { agent: Agent; returns?: any }): AgentHandoff {\n return {\n agent: options.agent,\n returns: options.returns,\n [HANDOFF_SYMBOL]: true,\n };\n}\n\nexport interface ToolOptions<UserData = UnknownUserData> {\n /**\n * RunContext for the current agent session.\n */\n ctx: RunContext<UserData>;\n\n /**\n * The ID of the tool call.\n */\n toolCallId: string;\n\n /**\n * An optional abort signal that indicates that the overall operation should be aborted.\n */\n abortSignal?: AbortSignal;\n}\n\nexport type ToolExecuteFunction<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> = (args: Parameters, opts: ToolOptions<UserData>) => Promise<Result>;\n\nexport interface Tool {\n /**\n * The type of the tool.\n * @internal Either user-defined core tool or provider-defined tool.\n */\n type: ToolType;\n\n [TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools\nexport interface ProviderDefinedTool extends Tool {\n type: 'provider-defined';\n\n /**\n * The ID of the tool.\n */\n id: string;\n\n /**\n * The configuration of the tool.\n */\n config: Record<string, unknown>;\n\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true;\n}\n\nexport interface FunctionTool<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> extends Tool {\n type: 'function';\n\n /**\n * The description of the tool. Will be used by the language model to decide whether to use the tool.\n */\n description: string;\n\n /**\n * The schema of the input that the tool expects. The language model will use this to generate the input.\n * It is also used to validate the output of the language model.\n * Use descriptions to make the input understandable for the language model.\n */\n parameters: ToolInputSchema<Parameters>;\n\n /**\n * An async function that is called with the arguments from the tool call and produces a result.\n * It also carries context about current session, user-defined data, and the tool call id, etc.\n */\n execute: ToolExecuteFunction<Parameters, UserData, Result>;\n\n [FUNCTION_TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools in the future)\nexport type ToolContext<UserData = UnknownUserData> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic tool registry needs to accept any parameter/result types\n [name: string]: FunctionTool<any, UserData, any>;\n};\n\nexport function isSameToolContext(ctx1: ToolContext, ctx2: ToolContext): boolean {\n const toolNames = new Set(Object.keys(ctx1));\n const toolNames2 = new Set(Object.keys(ctx2));\n\n if (toolNames.size !== toolNames2.size) {\n return false;\n }\n\n for (const name of toolNames) {\n if (!toolNames2.has(name)) {\n return false;\n }\n\n const tool1 = ctx1[name];\n const tool2 = ctx2[name];\n\n if (!tool1 || !tool2) {\n return false;\n }\n\n if (tool1.description !== tool2.description) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function isSameToolChoice(choice1: ToolChoice | null, choice2: ToolChoice | null): boolean {\n if (choice1 === choice2) {\n return true;\n }\n if (choice1 === null || choice2 === null) {\n return false;\n }\n if (typeof choice1 === 'string' && typeof choice2 === 'string') {\n return choice1 === choice2;\n }\n if (typeof choice1 === 'object' && typeof choice2 === 'object') {\n return choice1.type === choice2.type && choice1.function.name === choice2.function.name;\n }\n return false;\n}\n\n/**\n * Create a function tool with inferred parameters from the schema.\n */\nexport function tool<\n Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type\n UserData = UnknownUserData,\n Result = unknown,\n>({\n description,\n parameters,\n execute,\n}: {\n description: string;\n parameters: Schema;\n execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;\n}): FunctionTool<InferToolInput<Schema>, UserData, Result>;\n\n/**\n * Create a function tool without parameters.\n */\nexport function tool<UserData = UnknownUserData, Result = unknown>({\n description,\n execute,\n}: {\n description: string;\n parameters?: never;\n execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;\n}): FunctionTool<Record<string, never>, UserData, Result>;\n\n/**\n * Create a provider-defined tool.\n *\n * @param id - The ID of the tool.\n * @param config - The configuration of the tool.\n */\nexport function tool({\n id,\n config,\n}: {\n id: string;\n config: Record<string, unknown>;\n}): ProviderDefinedTool;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tool(tool: any): any {\n if (tool.execute !== undefined) {\n // Default parameters to z.object({}) if not provided\n const parameters = tool.parameters ?? z.object({});\n\n // if parameters is a Zod schema, ensure it's an object schema\n if (isZodSchema(parameters) && !isZodObjectSchema(parameters)) {\n throw new Error('Tool parameters must be a Zod object schema (z.object(...))');\n }\n\n // Ensure parameters is either a Zod schema or a plain object (JSON schema)\n if (!isZodSchema(parameters) && !(typeof parameters === 'object')) {\n throw new Error('Tool parameters must be a Zod object schema or a raw JSON schema');\n }\n\n return {\n type: 'function',\n description: tool.description,\n parameters,\n execute: tool.execute,\n [TOOL_SYMBOL]: true,\n [FUNCTION_TOOL_SYMBOL]: true,\n };\n }\n\n if (tool.config !== undefined && tool.id !== undefined) {\n return {\n type: 'provider-defined',\n id: tool.id,\n config: tool.config,\n [TOOL_SYMBOL]: true,\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true,\n };\n }\n\n throw new Error('Invalid tool');\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isTool(tool: any): tool is Tool {\n return tool && tool[TOOL_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isFunctionTool(tool: any): tool is FunctionTool<any, any, any> {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isFunctionTool = tool[FUNCTION_TOOL_SYMBOL] === true;\n return isTool && isFunctionTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isProviderDefinedTool(tool: any): tool is ProviderDefinedTool {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isProviderDefinedTool = tool[PROVIDER_DEFINED_TOOL_SYMBOL] === true;\n return isTool && isProviderDefinedTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isToolError(error: any): error is ToolError {\n return error && error[TOOL_ERROR_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isAgentHandoff(handoff: any): handoff is AgentHandoff {\n return handoff && handoff[HANDOFF_SYMBOL] === true;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,iBAAkB;AAGlB,uBAA+C;AAK/C,MAAM,cAAc,OAAO,MAAM;AACjC,MAAM,uBAAuB,OAAO,eAAe;AACnD,MAAM,+BAA+B,OAAO,uBAAuB;AACnE,MAAM,oBAAoB,OAAO,YAAY;AAC7C,MAAM,iBAAiB,OAAO,SAAS;AAwDhC,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,SAAiB;AAC3B,UAAM,OAAO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AY,UAAU,KAAK,KAAC,oCAAkB,UAAU,GAAG;AAC7D,YAAM,IAAI,MAAM,6DAA6D;AAAA,IAC/E;AAGA,QAAI,KAAC,8BAAY,UAAU,KAAK,EAAE,OAAO,eAAe,WAAW;AACjE,YAAM,IAAI,MAAM,kEAAkE;AAAA,IACpF;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN,aAAaA,MAAK;AAAA,MAClB;AAAA,MACA,SAASA,MAAK;AAAA,MACd,CAAC,WAAW,GAAG;AAAA,MACf,CAAC,oBAAoB,GAAG;AAAA,IAC1B;AAAA,EACF;AAEA,MAAIA,MAAK,WAAW,UAAaA,MAAK,OAAO,QAAW;AACtD,WAAO;AAAA,MACL,MAAM;AAAA,MACN,IAAIA,MAAK;AAAA,MACT,QAAQA,MAAK;AAAA,MACb,CAAC,WAAW,GAAG;AAAA,MACf,CAAC,4BAA4B,GAAG;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,IAAI,MAAM,cAAc;AAChC;AAGO,SAAS,OAAOA,OAAyB;AAC9C,SAAOA,SAAQA,MAAK,WAAW,MAAM;AACvC;AAGO,SAAS,eAAeA,OAAgD;AAC7E,QAAMC,UAASD,SAAQA,MAAK,WAAW,MAAM;AAC7C,QAAME,kBAAiBF,MAAK,oBAAoB,MAAM;AACtD,SAAOC,WAAUC;AACnB;AAGO,SAAS,sBAAsBF,OAAwC;AAC5E,QAAMC,UAASD,SAAQA,MAAK,WAAW,MAAM;AAC7C,QAAMG,yBAAwBH,MAAK,4BAA4B,MAAM;AACrE,SAAOC,WAAUE;AACnB;AAGO,SAAS,YAAY,OAAgC;AAC1D,SAAO,SAAS,MAAM,iBAAiB,MAAM;AAC/C;AAGO,SAAS,eAAeC,UAAuC;AACpE,SAAOA,YAAWA,SAAQ,cAAc,MAAM;AAChD;","names":["tool","isTool","isFunctionTool","isProviderDefinedTool","handoff"]}
|
|
1
|
+
{"version":3,"sources":["../../src/llm/tool_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { Agent } from '../voice/agent.js';\nimport type { RunContext, UnknownUserData } from '../voice/run_context.js';\nimport { isZodObjectSchema, isZodSchema } from './zod-utils.js';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\nconst TOOL_SYMBOL = Symbol('tool');\nconst FUNCTION_TOOL_SYMBOL = Symbol('function_tool');\nconst PROVIDER_DEFINED_TOOL_SYMBOL = Symbol('provider_defined_tool');\nconst TOOL_ERROR_SYMBOL = Symbol('tool_error');\nconst HANDOFF_SYMBOL = Symbol('handoff');\n\nexport type JSONValue = null | string | number | boolean | JSONObject | JSONArray;\n\nexport type JSONArray = JSONValue[];\n\nexport type JSONObject = {\n [key: string]: JSONValue;\n};\n\n// Supports both Zod v3 and v4 schemas, as well as raw JSON schema\n// Adapted from Vercel AI SDK's FlexibleSchema approach\n// Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L67-L70\n//\n// Vercel uses StandardSchemaV1 from @standard-schema/spec package.\n// We use a simpler approach by directly checking for schema properties:\n// - Zod v3: Has `_output` property\n// - Zod v4: Implements Standard Schema spec with `~standard` property\n// - JSON Schema: Plain object fallback\nexport type ToolInputSchema<T = JSONObject> =\n | {\n // Zod v3 schema - has _output property for type inference\n _output: T;\n }\n | {\n // Zod v4 schema (Standard Schema) - has ~standard property\n '~standard': {\n types?: { output: T };\n };\n }\n | JSONSchema7;\n\n/**\n * Infer the output type from a ToolInputSchema.\n * Adapted from Vercel AI SDK's InferSchema type.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L72-L79\n */\nexport type InferToolInput<T> = T extends { _output: infer O }\n ? O\n : T extends { '~standard': { types?: { output: infer O } } }\n ? O\n : any; // eslint-disable-line @typescript-eslint/no-explicit-any -- Fallback type for JSON Schema objects without type inference\n\nexport type ToolType = 'function' | 'provider-defined';\n\nexport type ToolChoice =\n | 'auto'\n | 'none'\n | 'required'\n | {\n type: 'function';\n function: {\n name: string;\n };\n };\n\nexport class ToolError extends Error {\n constructor(message: string) {\n super(message);\n\n Object.defineProperty(this, TOOL_ERROR_SYMBOL, {\n value: true,\n });\n }\n}\n\nexport const ToolFlag = {\n NONE: 0,\n IGNORE_ON_ENTER: 1 << 0,\n} as const;\n\nexport type ToolFlag = (typeof ToolFlag)[keyof typeof ToolFlag];\n\nexport interface AgentHandoff {\n /**\n * The agent to handoff to.\n */\n agent: Agent;\n\n /**\n * The return value of the tool.\n */\n returns?: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n [HANDOFF_SYMBOL]: true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handoff(options: { agent: Agent; returns?: any }): AgentHandoff {\n return {\n agent: options.agent,\n returns: options.returns,\n [HANDOFF_SYMBOL]: true,\n };\n}\n\nexport interface ToolOptions<UserData = UnknownUserData> {\n /**\n * RunContext for the current agent session.\n */\n ctx: RunContext<UserData>;\n\n /**\n * The ID of the tool call.\n */\n toolCallId: string;\n\n /**\n * An optional abort signal that indicates that the overall operation should be aborted.\n */\n abortSignal?: AbortSignal;\n}\n\nexport type ToolExecuteFunction<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> = (args: Parameters, opts: ToolOptions<UserData>) => Promise<Result>;\n\nexport interface Tool {\n /**\n * The type of the tool.\n * @internal Either user-defined core tool or provider-defined tool.\n */\n type: ToolType;\n\n [TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools\nexport interface ProviderDefinedTool extends Tool {\n type: 'provider-defined';\n\n /**\n * The ID of the tool.\n */\n id: string;\n\n /**\n * The configuration of the tool.\n */\n config: Record<string, unknown>;\n\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true;\n}\n\nexport interface FunctionTool<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> extends Tool {\n type: 'function';\n\n /**\n * The description of the tool. Will be used by the language model to decide whether to use the tool.\n */\n description: string;\n\n /**\n * The schema of the input that the tool expects. The language model will use this to generate the input.\n * It is also used to validate the output of the language model.\n * Use descriptions to make the input understandable for the language model.\n */\n parameters: ToolInputSchema<Parameters>;\n\n /**\n * An async function that is called with the arguments from the tool call and produces a result.\n * It also carries context about current session, user-defined data, and the tool call id, etc.\n */\n execute: ToolExecuteFunction<Parameters, UserData, Result>;\n\n flags: number;\n\n [FUNCTION_TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools in the future)\nexport type ToolContext<UserData = UnknownUserData> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic tool registry needs to accept any parameter/result types\n [name: string]: FunctionTool<any, UserData, any>;\n};\n\nexport function isSameToolContext(ctx1: ToolContext, ctx2: ToolContext): boolean {\n const toolNames = new Set(Object.keys(ctx1));\n const toolNames2 = new Set(Object.keys(ctx2));\n\n if (toolNames.size !== toolNames2.size) {\n return false;\n }\n\n for (const name of toolNames) {\n if (!toolNames2.has(name)) {\n return false;\n }\n\n const tool1 = ctx1[name];\n const tool2 = ctx2[name];\n\n if (!tool1 || !tool2) {\n return false;\n }\n\n if (tool1.description !== tool2.description) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function isSameToolChoice(choice1: ToolChoice | null, choice2: ToolChoice | null): boolean {\n if (choice1 === choice2) {\n return true;\n }\n if (choice1 === null || choice2 === null) {\n return false;\n }\n if (typeof choice1 === 'string' && typeof choice2 === 'string') {\n return choice1 === choice2;\n }\n if (typeof choice1 === 'object' && typeof choice2 === 'object') {\n return choice1.type === choice2.type && choice1.function.name === choice2.function.name;\n }\n return false;\n}\n\n/**\n * Create a function tool with inferred parameters from the schema.\n */\nexport function tool<\n Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type\n UserData = UnknownUserData,\n Result = unknown,\n>({\n description,\n parameters,\n execute,\n flags,\n}: {\n description: string;\n parameters: Schema;\n execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;\n flags?: number;\n}): FunctionTool<InferToolInput<Schema>, UserData, Result>;\n\n/**\n * Create a function tool without parameters.\n */\nexport function tool<UserData = UnknownUserData, Result = unknown>({\n description,\n execute,\n flags,\n}: {\n description: string;\n parameters?: never;\n execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;\n flags?: number;\n}): FunctionTool<Record<string, never>, UserData, Result>;\n\n/**\n * Create a provider-defined tool.\n *\n * @param id - The ID of the tool.\n * @param config - The configuration of the tool.\n */\nexport function tool({\n id,\n config,\n}: {\n id: string;\n config: Record<string, unknown>;\n}): ProviderDefinedTool;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tool(tool: any): any {\n if (tool.execute !== undefined) {\n // Default parameters to z.object({}) if not provided\n const parameters = tool.parameters ?? z.object({});\n\n // if parameters is a Zod schema, ensure it's an object schema\n if (isZodSchema(parameters) && !isZodObjectSchema(parameters)) {\n throw new Error('Tool parameters must be a Zod object schema (z.object(...))');\n }\n\n // Ensure parameters is either a Zod schema or a plain object (JSON schema)\n if (!isZodSchema(parameters) && !(typeof parameters === 'object')) {\n throw new Error('Tool parameters must be a Zod object schema or a raw JSON schema');\n }\n\n return {\n type: 'function',\n description: tool.description,\n parameters,\n execute: tool.execute,\n flags: tool.flags ?? ToolFlag.NONE,\n [TOOL_SYMBOL]: true,\n [FUNCTION_TOOL_SYMBOL]: true,\n };\n }\n\n if (tool.config !== undefined && tool.id !== undefined) {\n return {\n type: 'provider-defined',\n id: tool.id,\n config: tool.config,\n [TOOL_SYMBOL]: true,\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true,\n };\n }\n\n throw new Error('Invalid tool');\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isTool(tool: any): tool is Tool {\n return tool && tool[TOOL_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isFunctionTool(tool: any): tool is FunctionTool<any, any, any> {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isFunctionTool = tool[FUNCTION_TOOL_SYMBOL] === true;\n return isTool && isFunctionTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isProviderDefinedTool(tool: any): tool is ProviderDefinedTool {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isProviderDefinedTool = tool[PROVIDER_DEFINED_TOOL_SYMBOL] === true;\n return isTool && isProviderDefinedTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isToolError(error: any): error is ToolError {\n return error && error[TOOL_ERROR_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isAgentHandoff(handoff: any): handoff is AgentHandoff {\n return handoff && handoff[HANDOFF_SYMBOL] === true;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,iBAAkB;AAGlB,uBAA+C;AAK/C,MAAM,cAAc,OAAO,MAAM;AACjC,MAAM,uBAAuB,OAAO,eAAe;AACnD,MAAM,+BAA+B,OAAO,uBAAuB;AACnE,MAAM,oBAAoB,OAAO,YAAY;AAC7C,MAAM,iBAAiB,OAAO,SAAS;AAwDhC,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,WAAO,eAAe,MAAM,mBAAmB;AAAA,MAC7C,OAAO;AAAA,IACT,CAAC;AAAA,EACH;AACF;AAEO,MAAM,WAAW;AAAA,EACtB,MAAM;AAAA,EACN,iBAAiB,KAAK;AACxB;AAmBO,SAAS,QAAQ,SAAwD;AAC9E,SAAO;AAAA,IACL,OAAO,QAAQ;AAAA,IACf,SAAS,QAAQ;AAAA,IACjB,CAAC,cAAc,GAAG;A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;AAAA,MACL,MAAM;AAAA,MACN,aAAaA,MAAK;AAAA,MAClB;AAAA,MACA,SAASA,MAAK;AAAA,MACd,OAAOA,MAAK,SAAS,SAAS;AAAA,MAC9B,CAAC,WAAW,GAAG;AAAA,MACf,CAAC,oBAAoB,GAAG;AAAA,IAC1B;AAAA,EACF;AAEA,MAAIA,MAAK,WAAW,UAAaA,MAAK,OAAO,QAAW;AACtD,WAAO;AAAA,MACL,MAAM;AAAA,MACN,IAAIA,MAAK;AAAA,MACT,QAAQA,MAAK;AAAA,MACb,CAAC,WAAW,GAAG;AAAA,MACf,CAAC,4BAA4B,GAAG;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,IAAI,MAAM,cAAc;AAChC;AAGO,SAAS,OAAOA,OAAyB;AAC9C,SAAOA,SAAQA,MAAK,WAAW,MAAM;AACvC;AAGO,SAAS,eAAeA,OAAgD;AAC7E,QAAMC,UAASD,SAAQA,MAAK,WAAW,MAAM;AAC7C,QAAME,kBAAiBF,MAAK,oBAAoB,MAAM;AACtD,SAAOC,WAAUC;AACnB;AAGO,SAAS,sBAAsBF,OAAwC;AAC5E,QAAMC,UAASD,SAAQA,MAAK,WAAW,MAAM;AAC7C,QAAMG,yBAAwBH,MAAK,4BAA4B,MAAM;AACrE,SAAOC,WAAUE;AACnB;AAGO,SAAS,YAAY,OAAgC;AAC1D,SAAO,SAAS,MAAM,iBAAiB,MAAM;AAC/C;AAGO,SAAS,eAAeC,UAAuC;AACpE,SAAOA,YAAWA,SAAQ,cAAc,MAAM;AAChD;","names":["tool","isTool","isFunctionTool","isProviderDefinedTool","handoff"]}
|
|
@@ -43,6 +43,11 @@ export type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
|
43
43
|
export declare class ToolError extends Error {
|
|
44
44
|
constructor(message: string);
|
|
45
45
|
}
|
|
46
|
+
export declare const ToolFlag: {
|
|
47
|
+
readonly NONE: 0;
|
|
48
|
+
readonly IGNORE_ON_ENTER: number;
|
|
49
|
+
};
|
|
50
|
+
export type ToolFlag = (typeof ToolFlag)[keyof typeof ToolFlag];
|
|
46
51
|
export interface AgentHandoff {
|
|
47
52
|
/**
|
|
48
53
|
* The agent to handoff to.
|
|
@@ -110,6 +115,7 @@ export interface FunctionTool<Parameters extends JSONObject, UserData = UnknownU
|
|
|
110
115
|
* It also carries context about current session, user-defined data, and the tool call id, etc.
|
|
111
116
|
*/
|
|
112
117
|
execute: ToolExecuteFunction<Parameters, UserData, Result>;
|
|
118
|
+
flags: number;
|
|
113
119
|
[FUNCTION_TOOL_SYMBOL]: true;
|
|
114
120
|
}
|
|
115
121
|
export type ToolContext<UserData = UnknownUserData> = {
|
|
@@ -121,18 +127,20 @@ export declare function isSameToolChoice(choice1: ToolChoice | null, choice2: To
|
|
|
121
127
|
* Create a function tool with inferred parameters from the schema.
|
|
122
128
|
*/
|
|
123
129
|
export declare function tool<Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type
|
|
124
|
-
UserData = UnknownUserData, Result = unknown>({ description, parameters, execute, }: {
|
|
130
|
+
UserData = UnknownUserData, Result = unknown>({ description, parameters, execute, flags, }: {
|
|
125
131
|
description: string;
|
|
126
132
|
parameters: Schema;
|
|
127
133
|
execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;
|
|
134
|
+
flags?: number;
|
|
128
135
|
}): FunctionTool<InferToolInput<Schema>, UserData, Result>;
|
|
129
136
|
/**
|
|
130
137
|
* Create a function tool without parameters.
|
|
131
138
|
*/
|
|
132
|
-
export declare function tool<UserData = UnknownUserData, Result = unknown>({ description, execute, }: {
|
|
139
|
+
export declare function tool<UserData = UnknownUserData, Result = unknown>({ description, execute, flags, }: {
|
|
133
140
|
description: string;
|
|
134
141
|
parameters?: never;
|
|
135
142
|
execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;
|
|
143
|
+
flags?: number;
|
|
136
144
|
}): FunctionTool<Record<string, never>, UserData, Result>;
|
|
137
145
|
/**
|
|
138
146
|
* Create a provider-defined tool.
|
|
@@ -43,6 +43,11 @@ export type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
|
43
43
|
export declare class ToolError extends Error {
|
|
44
44
|
constructor(message: string);
|
|
45
45
|
}
|
|
46
|
+
export declare const ToolFlag: {
|
|
47
|
+
readonly NONE: 0;
|
|
48
|
+
readonly IGNORE_ON_ENTER: number;
|
|
49
|
+
};
|
|
50
|
+
export type ToolFlag = (typeof ToolFlag)[keyof typeof ToolFlag];
|
|
46
51
|
export interface AgentHandoff {
|
|
47
52
|
/**
|
|
48
53
|
* The agent to handoff to.
|
|
@@ -110,6 +115,7 @@ export interface FunctionTool<Parameters extends JSONObject, UserData = UnknownU
|
|
|
110
115
|
* It also carries context about current session, user-defined data, and the tool call id, etc.
|
|
111
116
|
*/
|
|
112
117
|
execute: ToolExecuteFunction<Parameters, UserData, Result>;
|
|
118
|
+
flags: number;
|
|
113
119
|
[FUNCTION_TOOL_SYMBOL]: true;
|
|
114
120
|
}
|
|
115
121
|
export type ToolContext<UserData = UnknownUserData> = {
|
|
@@ -121,18 +127,20 @@ export declare function isSameToolChoice(choice1: ToolChoice | null, choice2: To
|
|
|
121
127
|
* Create a function tool with inferred parameters from the schema.
|
|
122
128
|
*/
|
|
123
129
|
export declare function tool<Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type
|
|
124
|
-
UserData = UnknownUserData, Result = unknown>({ description, parameters, execute, }: {
|
|
130
|
+
UserData = UnknownUserData, Result = unknown>({ description, parameters, execute, flags, }: {
|
|
125
131
|
description: string;
|
|
126
132
|
parameters: Schema;
|
|
127
133
|
execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;
|
|
134
|
+
flags?: number;
|
|
128
135
|
}): FunctionTool<InferToolInput<Schema>, UserData, Result>;
|
|
129
136
|
/**
|
|
130
137
|
* Create a function tool without parameters.
|
|
131
138
|
*/
|
|
132
|
-
export declare function tool<UserData = UnknownUserData, Result = unknown>({ description, execute, }: {
|
|
139
|
+
export declare function tool<UserData = UnknownUserData, Result = unknown>({ description, execute, flags, }: {
|
|
133
140
|
description: string;
|
|
134
141
|
parameters?: never;
|
|
135
142
|
execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;
|
|
143
|
+
flags?: number;
|
|
136
144
|
}): FunctionTool<Record<string, never>, UserData, Result>;
|
|
137
145
|
/**
|
|
138
146
|
* Create a provider-defined tool.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool_context.d.ts","sourceRoot":"","sources":["../../src/llm/tool_context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAM3E,QAAA,MAAM,WAAW,eAAiB,CAAC;AACnC,QAAA,MAAM,oBAAoB,eAA0B,CAAC;AACrD,QAAA,MAAM,4BAA4B,eAAkC,CAAC;AAErE,QAAA,MAAM,cAAc,eAAoB,CAAC;AAEzC,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,GAAG,SAAS,CAAC;AAElF,MAAM,MAAM,SAAS,GAAG,SAAS,EAAE,CAAC;AAEpC,MAAM,MAAM,UAAU,GAAG;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B,CAAC;AAWF,MAAM,MAAM,eAAe,CAAC,CAAC,GAAG,UAAU,IACtC;IAEE,OAAO,EAAE,CAAC,CAAC;CACZ,GACD;IAEE,WAAW,EAAE;QACX,KAAK,CAAC,EAAE;YAAE,MAAM,EAAE,CAAC,CAAA;SAAE,CAAC;KACvB,CAAC;CACH,GACD,WAAW,CAAC;AAEhB;;;;GAIG;AACH,MAAM,MAAM,cAAc,CAAC,CA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AAG,KAAK,IAAI,SAAS,CAE1D;AAGD,wBAAgB,cAAc,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,IAAI,YAAY,CAEpE"}
|
package/dist/llm/tool_context.js
CHANGED
|
@@ -13,6 +13,10 @@ class ToolError extends Error {
|
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
const ToolFlag = {
|
|
17
|
+
NONE: 0,
|
|
18
|
+
IGNORE_ON_ENTER: 1 << 0
|
|
19
|
+
};
|
|
16
20
|
function handoff(options) {
|
|
17
21
|
return {
|
|
18
22
|
agent: options.agent,
|
|
@@ -70,6 +74,7 @@ function tool(tool2) {
|
|
|
70
74
|
description: tool2.description,
|
|
71
75
|
parameters,
|
|
72
76
|
execute: tool2.execute,
|
|
77
|
+
flags: tool2.flags ?? ToolFlag.NONE,
|
|
73
78
|
[TOOL_SYMBOL]: true,
|
|
74
79
|
[FUNCTION_TOOL_SYMBOL]: true
|
|
75
80
|
};
|
|
@@ -106,6 +111,7 @@ function isAgentHandoff(handoff2) {
|
|
|
106
111
|
}
|
|
107
112
|
export {
|
|
108
113
|
ToolError,
|
|
114
|
+
ToolFlag,
|
|
109
115
|
handoff,
|
|
110
116
|
isAgentHandoff,
|
|
111
117
|
isFunctionTool,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/llm/tool_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { Agent } from '../voice/agent.js';\nimport type { RunContext, UnknownUserData } from '../voice/run_context.js';\nimport { isZodObjectSchema, isZodSchema } from './zod-utils.js';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\nconst TOOL_SYMBOL = Symbol('tool');\nconst FUNCTION_TOOL_SYMBOL = Symbol('function_tool');\nconst PROVIDER_DEFINED_TOOL_SYMBOL = Symbol('provider_defined_tool');\nconst TOOL_ERROR_SYMBOL = Symbol('tool_error');\nconst HANDOFF_SYMBOL = Symbol('handoff');\n\nexport type JSONValue = null | string | number | boolean | JSONObject | JSONArray;\n\nexport type JSONArray = JSONValue[];\n\nexport type JSONObject = {\n [key: string]: JSONValue;\n};\n\n// Supports both Zod v3 and v4 schemas, as well as raw JSON schema\n// Adapted from Vercel AI SDK's FlexibleSchema approach\n// Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L67-L70\n//\n// Vercel uses StandardSchemaV1 from @standard-schema/spec package.\n// We use a simpler approach by directly checking for schema properties:\n// - Zod v3: Has `_output` property\n// - Zod v4: Implements Standard Schema spec with `~standard` property\n// - JSON Schema: Plain object fallback\nexport type ToolInputSchema<T = JSONObject> =\n | {\n // Zod v3 schema - has _output property for type inference\n _output: T;\n }\n | {\n // Zod v4 schema (Standard Schema) - has ~standard property\n '~standard': {\n types?: { output: T };\n };\n }\n | JSONSchema7;\n\n/**\n * Infer the output type from a ToolInputSchema.\n * Adapted from Vercel AI SDK's InferSchema type.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L72-L79\n */\nexport type InferToolInput<T> = T extends { _output: infer O }\n ? O\n : T extends { '~standard': { types?: { output: infer O } } }\n ? O\n : any; // eslint-disable-line @typescript-eslint/no-explicit-any -- Fallback type for JSON Schema objects without type inference\n\nexport type ToolType = 'function' | 'provider-defined';\n\nexport type ToolChoice =\n | 'auto'\n | 'none'\n | 'required'\n | {\n type: 'function';\n function: {\n name: string;\n };\n };\n\nexport class ToolError extends Error {\n constructor(message: string) {\n super(message);\n\n Object.defineProperty(this, TOOL_ERROR_SYMBOL, {\n value: true,\n });\n }\n}\n\nexport interface AgentHandoff {\n /**\n * The agent to handoff to.\n */\n agent: Agent;\n\n /**\n * The return value of the tool.\n */\n returns?: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n [HANDOFF_SYMBOL]: true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handoff(options: { agent: Agent; returns?: any }): AgentHandoff {\n return {\n agent: options.agent,\n returns: options.returns,\n [HANDOFF_SYMBOL]: true,\n };\n}\n\nexport interface ToolOptions<UserData = UnknownUserData> {\n /**\n * RunContext for the current agent session.\n */\n ctx: RunContext<UserData>;\n\n /**\n * The ID of the tool call.\n */\n toolCallId: string;\n\n /**\n * An optional abort signal that indicates that the overall operation should be aborted.\n */\n abortSignal?: AbortSignal;\n}\n\nexport type ToolExecuteFunction<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> = (args: Parameters, opts: ToolOptions<UserData>) => Promise<Result>;\n\nexport interface Tool {\n /**\n * The type of the tool.\n * @internal Either user-defined core tool or provider-defined tool.\n */\n type: ToolType;\n\n [TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools\nexport interface ProviderDefinedTool extends Tool {\n type: 'provider-defined';\n\n /**\n * The ID of the tool.\n */\n id: string;\n\n /**\n * The configuration of the tool.\n */\n config: Record<string, unknown>;\n\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true;\n}\n\nexport interface FunctionTool<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> extends Tool {\n type: 'function';\n\n /**\n * The description of the tool. Will be used by the language model to decide whether to use the tool.\n */\n description: string;\n\n /**\n * The schema of the input that the tool expects. The language model will use this to generate the input.\n * It is also used to validate the output of the language model.\n * Use descriptions to make the input understandable for the language model.\n */\n parameters: ToolInputSchema<Parameters>;\n\n /**\n * An async function that is called with the arguments from the tool call and produces a result.\n * It also carries context about current session, user-defined data, and the tool call id, etc.\n */\n execute: ToolExecuteFunction<Parameters, UserData, Result>;\n\n [FUNCTION_TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools in the future)\nexport type ToolContext<UserData = UnknownUserData> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic tool registry needs to accept any parameter/result types\n [name: string]: FunctionTool<any, UserData, any>;\n};\n\nexport function isSameToolContext(ctx1: ToolContext, ctx2: ToolContext): boolean {\n const toolNames = new Set(Object.keys(ctx1));\n const toolNames2 = new Set(Object.keys(ctx2));\n\n if (toolNames.size !== toolNames2.size) {\n return false;\n }\n\n for (const name of toolNames) {\n if (!toolNames2.has(name)) {\n return false;\n }\n\n const tool1 = ctx1[name];\n const tool2 = ctx2[name];\n\n if (!tool1 || !tool2) {\n return false;\n }\n\n if (tool1.description !== tool2.description) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function isSameToolChoice(choice1: ToolChoice | null, choice2: ToolChoice | null): boolean {\n if (choice1 === choice2) {\n return true;\n }\n if (choice1 === null || choice2 === null) {\n return false;\n }\n if (typeof choice1 === 'string' && typeof choice2 === 'string') {\n return choice1 === choice2;\n }\n if (typeof choice1 === 'object' && typeof choice2 === 'object') {\n return choice1.type === choice2.type && choice1.function.name === choice2.function.name;\n }\n return false;\n}\n\n/**\n * Create a function tool with inferred parameters from the schema.\n */\nexport function tool<\n Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type\n UserData = UnknownUserData,\n Result = unknown,\n>({\n description,\n parameters,\n execute,\n}: {\n description: string;\n parameters: Schema;\n execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;\n}): FunctionTool<InferToolInput<Schema>, UserData, Result>;\n\n/**\n * Create a function tool without parameters.\n */\nexport function tool<UserData = UnknownUserData, Result = unknown>({\n description,\n execute,\n}: {\n description: string;\n parameters?: never;\n execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;\n}): FunctionTool<Record<string, never>, UserData, Result>;\n\n/**\n * Create a provider-defined tool.\n *\n * @param id - The ID of the tool.\n * @param config - The configuration of the tool.\n */\nexport function tool({\n id,\n config,\n}: {\n id: string;\n config: Record<string, unknown>;\n}): ProviderDefinedTool;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tool(tool: any): any {\n if (tool.execute !== undefined) {\n // Default parameters to z.object({}) if not provided\n const parameters = tool.parameters ?? z.object({});\n\n // if parameters is a Zod schema, ensure it's an object schema\n if (isZodSchema(parameters) && !isZodObjectSchema(parameters)) {\n throw new Error('Tool parameters must be a Zod object schema (z.object(...))');\n }\n\n // Ensure parameters is either a Zod schema or a plain object (JSON schema)\n if (!isZodSchema(parameters) && !(typeof parameters === 'object')) {\n throw new Error('Tool parameters must be a Zod object schema or a raw JSON schema');\n }\n\n return {\n type: 'function',\n description: tool.description,\n parameters,\n execute: tool.execute,\n [TOOL_SYMBOL]: true,\n [FUNCTION_TOOL_SYMBOL]: true,\n };\n }\n\n if (tool.config !== undefined && tool.id !== undefined) {\n return {\n type: 'provider-defined',\n id: tool.id,\n config: tool.config,\n [TOOL_SYMBOL]: true,\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true,\n };\n }\n\n throw new Error('Invalid tool');\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isTool(tool: any): tool is Tool {\n return tool && tool[TOOL_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isFunctionTool(tool: any): tool is FunctionTool<any, any, any> {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isFunctionTool = tool[FUNCTION_TOOL_SYMBOL] === true;\n return isTool && isFunctionTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isProviderDefinedTool(tool: any): tool is ProviderDefinedTool {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isProviderDefinedTool = tool[PROVIDER_DEFINED_TOOL_SYMBOL] === true;\n return isTool && isProviderDefinedTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isToolError(error: any): error is ToolError {\n return error && error[TOOL_ERROR_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isAgentHandoff(handoff: any): handoff is AgentHandoff {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M;AAC7C,QAAMG,yBAAwBH,MAAK,4BAA4B,MAAM;AACrE,SAAOC,WAAUE;AACnB;AAGO,SAAS,YAAY,OAAgC;AAC1D,SAAO,SAAS,MAAM,iBAAiB,MAAM;AAC/C;AAGO,SAAS,eAAeC,UAAuC;AACpE,SAAOA,YAAWA,SAAQ,cAAc,MAAM;AAChD;","names":["tool","isTool","isFunctionTool","isProviderDefinedTool","handoff"]}
|
|
1
|
+
{"version":3,"sources":["../../src/llm/tool_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { Agent } from '../voice/agent.js';\nimport type { RunContext, UnknownUserData } from '../voice/run_context.js';\nimport { isZodObjectSchema, isZodSchema } from './zod-utils.js';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\nconst TOOL_SYMBOL = Symbol('tool');\nconst FUNCTION_TOOL_SYMBOL = Symbol('function_tool');\nconst PROVIDER_DEFINED_TOOL_SYMBOL = Symbol('provider_defined_tool');\nconst TOOL_ERROR_SYMBOL = Symbol('tool_error');\nconst HANDOFF_SYMBOL = Symbol('handoff');\n\nexport type JSONValue = null | string | number | boolean | JSONObject | JSONArray;\n\nexport type JSONArray = JSONValue[];\n\nexport type JSONObject = {\n [key: string]: JSONValue;\n};\n\n// Supports both Zod v3 and v4 schemas, as well as raw JSON schema\n// Adapted from Vercel AI SDK's FlexibleSchema approach\n// Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L67-L70\n//\n// Vercel uses StandardSchemaV1 from @standard-schema/spec package.\n// We use a simpler approach by directly checking for schema properties:\n// - Zod v3: Has `_output` property\n// - Zod v4: Implements Standard Schema spec with `~standard` property\n// - JSON Schema: Plain object fallback\nexport type ToolInputSchema<T = JSONObject> =\n | {\n // Zod v3 schema - has _output property for type inference\n _output: T;\n }\n | {\n // Zod v4 schema (Standard Schema) - has ~standard property\n '~standard': {\n types?: { output: T };\n };\n }\n | JSONSchema7;\n\n/**\n * Infer the output type from a ToolInputSchema.\n * Adapted from Vercel AI SDK's InferSchema type.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L72-L79\n */\nexport type InferToolInput<T> = T extends { _output: infer O }\n ? O\n : T extends { '~standard': { types?: { output: infer O } } }\n ? O\n : any; // eslint-disable-line @typescript-eslint/no-explicit-any -- Fallback type for JSON Schema objects without type inference\n\nexport type ToolType = 'function' | 'provider-defined';\n\nexport type ToolChoice =\n | 'auto'\n | 'none'\n | 'required'\n | {\n type: 'function';\n function: {\n name: string;\n };\n };\n\nexport class ToolError extends Error {\n constructor(message: string) {\n super(message);\n\n Object.defineProperty(this, TOOL_ERROR_SYMBOL, {\n value: true,\n });\n }\n}\n\nexport const ToolFlag = {\n NONE: 0,\n IGNORE_ON_ENTER: 1 << 0,\n} as const;\n\nexport type ToolFlag = (typeof ToolFlag)[keyof typeof ToolFlag];\n\nexport interface AgentHandoff {\n /**\n * The agent to handoff to.\n */\n agent: Agent;\n\n /**\n * The return value of the tool.\n */\n returns?: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n [HANDOFF_SYMBOL]: true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handoff(options: { agent: Agent; returns?: any }): AgentHandoff {\n return {\n agent: options.agent,\n returns: options.returns,\n [HANDOFF_SYMBOL]: true,\n };\n}\n\nexport interface ToolOptions<UserData = UnknownUserData> {\n /**\n * RunContext for the current agent session.\n */\n ctx: RunContext<UserData>;\n\n /**\n * The ID of the tool call.\n */\n toolCallId: string;\n\n /**\n * An optional abort signal that indicates that the overall operation should be aborted.\n */\n abortSignal?: AbortSignal;\n}\n\nexport type ToolExecuteFunction<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> = (args: Parameters, opts: ToolOptions<UserData>) => Promise<Result>;\n\nexport interface Tool {\n /**\n * The type of the tool.\n * @internal Either user-defined core tool or provider-defined tool.\n */\n type: ToolType;\n\n [TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools\nexport interface ProviderDefinedTool extends Tool {\n type: 'provider-defined';\n\n /**\n * The ID of the tool.\n */\n id: string;\n\n /**\n * The configuration of the tool.\n */\n config: Record<string, unknown>;\n\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true;\n}\n\nexport interface FunctionTool<\n Parameters extends JSONObject,\n UserData = UnknownUserData,\n Result = unknown,\n> extends Tool {\n type: 'function';\n\n /**\n * The description of the tool. Will be used by the language model to decide whether to use the tool.\n */\n description: string;\n\n /**\n * The schema of the input that the tool expects. The language model will use this to generate the input.\n * It is also used to validate the output of the language model.\n * Use descriptions to make the input understandable for the language model.\n */\n parameters: ToolInputSchema<Parameters>;\n\n /**\n * An async function that is called with the arguments from the tool call and produces a result.\n * It also carries context about current session, user-defined data, and the tool call id, etc.\n */\n execute: ToolExecuteFunction<Parameters, UserData, Result>;\n\n flags: number;\n\n [FUNCTION_TOOL_SYMBOL]: true;\n}\n\n// TODO(AJS-112): support provider-defined tools in the future)\nexport type ToolContext<UserData = UnknownUserData> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic tool registry needs to accept any parameter/result types\n [name: string]: FunctionTool<any, UserData, any>;\n};\n\nexport function isSameToolContext(ctx1: ToolContext, ctx2: ToolContext): boolean {\n const toolNames = new Set(Object.keys(ctx1));\n const toolNames2 = new Set(Object.keys(ctx2));\n\n if (toolNames.size !== toolNames2.size) {\n return false;\n }\n\n for (const name of toolNames) {\n if (!toolNames2.has(name)) {\n return false;\n }\n\n const tool1 = ctx1[name];\n const tool2 = ctx2[name];\n\n if (!tool1 || !tool2) {\n return false;\n }\n\n if (tool1.description !== tool2.description) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function isSameToolChoice(choice1: ToolChoice | null, choice2: ToolChoice | null): boolean {\n if (choice1 === choice2) {\n return true;\n }\n if (choice1 === null || choice2 === null) {\n return false;\n }\n if (typeof choice1 === 'string' && typeof choice2 === 'string') {\n return choice1 === choice2;\n }\n if (typeof choice1 === 'object' && typeof choice2 === 'object') {\n return choice1.type === choice2.type && choice1.function.name === choice2.function.name;\n }\n return false;\n}\n\n/**\n * Create a function tool with inferred parameters from the schema.\n */\nexport function tool<\n Schema extends ToolInputSchema<any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- Generic constraint needs to accept any JSONObject type\n UserData = UnknownUserData,\n Result = unknown,\n>({\n description,\n parameters,\n execute,\n flags,\n}: {\n description: string;\n parameters: Schema;\n execute: ToolExecuteFunction<InferToolInput<Schema>, UserData, Result>;\n flags?: number;\n}): FunctionTool<InferToolInput<Schema>, UserData, Result>;\n\n/**\n * Create a function tool without parameters.\n */\nexport function tool<UserData = UnknownUserData, Result = unknown>({\n description,\n execute,\n flags,\n}: {\n description: string;\n parameters?: never;\n execute: ToolExecuteFunction<Record<string, never>, UserData, Result>;\n flags?: number;\n}): FunctionTool<Record<string, never>, UserData, Result>;\n\n/**\n * Create a provider-defined tool.\n *\n * @param id - The ID of the tool.\n * @param config - The configuration of the tool.\n */\nexport function tool({\n id,\n config,\n}: {\n id: string;\n config: Record<string, unknown>;\n}): ProviderDefinedTool;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tool(tool: any): any {\n if (tool.execute !== undefined) {\n // Default parameters to z.object({}) if not provided\n const parameters = tool.parameters ?? z.object({});\n\n // if parameters is a Zod schema, ensure it's an object schema\n if (isZodSchema(parameters) && !isZodObjectSchema(parameters)) {\n throw new Error('Tool parameters must be a Zod object schema (z.object(...))');\n }\n\n // Ensure parameters is either a Zod schema or a plain object (JSON schema)\n if (!isZodSchema(parameters) && !(typeof parameters === 'object')) {\n throw new Error('Tool parameters must be a Zod object schema or a raw JSON schema');\n }\n\n return {\n type: 'function',\n description: tool.description,\n parameters,\n execute: tool.execute,\n flags: tool.flags ?? ToolFlag.NONE,\n [TOOL_SYMBOL]: true,\n [FUNCTION_TOOL_SYMBOL]: true,\n };\n }\n\n if (tool.config !== undefined && tool.id !== undefined) {\n return {\n type: 'provider-defined',\n id: tool.id,\n config: tool.config,\n [TOOL_SYMBOL]: true,\n [PROVIDER_DEFINED_TOOL_SYMBOL]: true,\n };\n }\n\n throw new Error('Invalid tool');\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isTool(tool: any): tool is Tool {\n return tool && tool[TOOL_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isFunctionTool(tool: any): tool is FunctionTool<any, any, any> {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isFunctionTool = tool[FUNCTION_TOOL_SYMBOL] === true;\n return isTool && isFunctionTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isProviderDefinedTool(tool: any): tool is ProviderDefinedTool {\n const isTool = tool && tool[TOOL_SYMBOL] === true;\n const isProviderDefinedTool = tool[PROVIDER_DEFINED_TOOL_SYMBOL] === true;\n return isTool && isProviderDefinedTool;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isToolError(error: any): error is ToolError {\n return error && error[TOOL_ERROR_SYMBOL] === true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isAgentHandoff(handoff: any): handoff is AgentHandoff {\n return handoff && handoff[HANDOFF_SYMBOL] === true;\n}\n"],"mappings":"AAIA,SAAS,SAAS;AAGlB,SAAS,mBAAmB,mBAAmB;AAK/C,MAAM,cAAc,OAAO,MAAM;AACjC,MAAM,uBAAuB,OAAO,eAAe;AACnD,MAAM,+BAA+B,OAAO,uBAAuB;AACnE,MAAM,oBAAoB,OAAO,YAAY;AAC7C,MAAM,iBAAiB,OAAO,SAAS;AAwDhC,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,WAAO,eAAe,MAAM,mBAAmB;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|
package/dist/log.cjs
CHANGED
|
@@ -43,7 +43,7 @@ const log = () => {
|
|
|
43
43
|
const initializeLogger = ({ pretty, level }) => {
|
|
44
44
|
globals[LOGGER_OPTIONS_KEY] = { pretty, level };
|
|
45
45
|
globals[LOGGER_KEY] = (0, import_pino.pino)(
|
|
46
|
-
{ level: level || "info" },
|
|
46
|
+
{ level: level || "info", serializers: { error: import_pino.pino.stdSerializers.err } },
|
|
47
47
|
pretty ? (0, import_pino_pretty.build)({ colorize: true }) : process.stdout
|
|
48
48
|
);
|
|
49
49
|
};
|
|
@@ -73,7 +73,10 @@ const enableOtelLogging = () => {
|
|
|
73
73
|
{ stream: pretty ? (0, import_pino_pretty.build)({ colorize: true }) : process.stdout, level: logLevel },
|
|
74
74
|
{ stream: new OtelDestination(), level: "debug" }
|
|
75
75
|
];
|
|
76
|
-
globals[LOGGER_KEY] = (0, import_pino.pino)(
|
|
76
|
+
globals[LOGGER_KEY] = (0, import_pino.pino)(
|
|
77
|
+
{ level: logLevel, serializers: { error: import_pino.pino.stdSerializers.err } },
|
|
78
|
+
(0, import_pino.multistream)(streams)
|
|
79
|
+
);
|
|
77
80
|
};
|
|
78
81
|
// Annotate the CommonJS export names for ESM import in node:
|
|
79
82
|
0 && (module.exports = {
|
package/dist/log.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/log.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Writable } from 'node:stream';\nimport type { DestinationStream, Logger } from 'pino';\nimport { multistream, pino } from 'pino';\nimport { build as pinoPretty } from 'pino-pretty';\nimport { type PinoLogObject, emitToOtel } from './telemetry/pino_otel_transport.js';\n\n/** @internal */\nexport type LoggerOptions = {\n pretty: boolean;\n level?: string;\n};\n\n// Use Symbol.for() + globalThis to create process-wide singletons.\n// This avoids the \"dual package hazard\". Symbol.for() returns the same Symbol\n// across all module instances, and globalThis is shared process-wide.\nconst LOGGER_KEY = Symbol.for('@livekit/agents:logger');\nconst LOGGER_OPTIONS_KEY = Symbol.for('@livekit/agents:loggerOptions');\nconst OTEL_ENABLED_KEY = Symbol.for('@livekit/agents:otelEnabled');\n\ntype GlobalState = {\n [LOGGER_KEY]?: Logger;\n [LOGGER_OPTIONS_KEY]?: LoggerOptions;\n [OTEL_ENABLED_KEY]?: boolean;\n};\n\nconst globals = globalThis as typeof globalThis & GlobalState;\n\n/** @internal */\nexport const loggerOptions = (): LoggerOptions | undefined => globals[LOGGER_OPTIONS_KEY];\n\n/** @internal */\nexport const log = () => {\n const logger = globals[LOGGER_KEY];\n if (!logger) {\n throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');\n }\n return logger;\n};\n\n/** @internal */\nexport const initializeLogger = ({ pretty, level }: LoggerOptions) => {\n globals[LOGGER_OPTIONS_KEY] = { pretty, level };\n globals[LOGGER_KEY] = pino(\n { level: level || 'info' },\n pretty ? pinoPretty({ colorize: true }) : process.stdout,\n );\n};\n\n/**\n * Custom Pino destination that parses JSON logs and emits to OTEL.\n * This receives the FULL serialized log including msg, level, time, etc.\n */\nclass OtelDestination extends Writable {\n _write(chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {\n try {\n const line = chunk.toString().trim();\n if (line) {\n const logObj = JSON.parse(line) as PinoLogObject;\n emitToOtel(logObj);\n }\n } catch {\n // Ignore parse errors (e.g., non-JSON lines)\n }\n callback();\n }\n}\n\n/**\n * Enable OTEL logging by reconfiguring the logger with multistream.\n * Uses a custom destination that receives full JSON logs (with msg, level, time).\n *\n * @internal\n */\nexport const enableOtelLogging = () => {\n if (globals[OTEL_ENABLED_KEY] || !globals[LOGGER_KEY]) {\n console.warn('OTEL logging already enabled or logger not initialized');\n return;\n }\n globals[OTEL_ENABLED_KEY] = true;\n\n const opts = globals[LOGGER_OPTIONS_KEY]!;\n const { pretty, level } = opts;\n\n const logLevel = level || 'info';\n const streams: { stream: DestinationStream; level: string }[] = [\n { stream: pretty ? pinoPretty({ colorize: true }) : process.stdout, level: logLevel },\n { stream: new OtelDestination(), level: 'debug' },\n ];\n\n globals[LOGGER_KEY] = pino({ level: logLevel }
|
|
1
|
+
{"version":3,"sources":["../src/log.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Writable } from 'node:stream';\nimport type { DestinationStream, Logger } from 'pino';\nimport { multistream, pino } from 'pino';\nimport { build as pinoPretty } from 'pino-pretty';\nimport { type PinoLogObject, emitToOtel } from './telemetry/pino_otel_transport.js';\n\n/** @internal */\nexport type LoggerOptions = {\n pretty: boolean;\n level?: string;\n};\n\n// Use Symbol.for() + globalThis to create process-wide singletons.\n// This avoids the \"dual package hazard\". Symbol.for() returns the same Symbol\n// across all module instances, and globalThis is shared process-wide.\nconst LOGGER_KEY = Symbol.for('@livekit/agents:logger');\nconst LOGGER_OPTIONS_KEY = Symbol.for('@livekit/agents:loggerOptions');\nconst OTEL_ENABLED_KEY = Symbol.for('@livekit/agents:otelEnabled');\n\ntype GlobalState = {\n [LOGGER_KEY]?: Logger;\n [LOGGER_OPTIONS_KEY]?: LoggerOptions;\n [OTEL_ENABLED_KEY]?: boolean;\n};\n\nconst globals = globalThis as typeof globalThis & GlobalState;\n\n/** @internal */\nexport const loggerOptions = (): LoggerOptions | undefined => globals[LOGGER_OPTIONS_KEY];\n\n/** @internal */\nexport const log = () => {\n const logger = globals[LOGGER_KEY];\n if (!logger) {\n throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');\n }\n return logger;\n};\n\n/** @internal */\nexport const initializeLogger = ({ pretty, level }: LoggerOptions) => {\n globals[LOGGER_OPTIONS_KEY] = { pretty, level };\n globals[LOGGER_KEY] = pino(\n { level: level || 'info', serializers: { error: pino.stdSerializers.err } },\n pretty ? pinoPretty({ colorize: true }) : process.stdout,\n );\n};\n\n/**\n * Custom Pino destination that parses JSON logs and emits to OTEL.\n * This receives the FULL serialized log including msg, level, time, etc.\n */\nclass OtelDestination extends Writable {\n _write(chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {\n try {\n const line = chunk.toString().trim();\n if (line) {\n const logObj = JSON.parse(line) as PinoLogObject;\n emitToOtel(logObj);\n }\n } catch {\n // Ignore parse errors (e.g., non-JSON lines)\n }\n callback();\n }\n}\n\n/**\n * Enable OTEL logging by reconfiguring the logger with multistream.\n * Uses a custom destination that receives full JSON logs (with msg, level, time).\n *\n * @internal\n */\nexport const enableOtelLogging = () => {\n if (globals[OTEL_ENABLED_KEY] || !globals[LOGGER_KEY]) {\n console.warn('OTEL logging already enabled or logger not initialized');\n return;\n }\n globals[OTEL_ENABLED_KEY] = true;\n\n const opts = globals[LOGGER_OPTIONS_KEY]!;\n const { pretty, level } = opts;\n\n const logLevel = level || 'info';\n const streams: { stream: DestinationStream; level: string }[] = [\n { stream: pretty ? pinoPretty({ colorize: true }) : process.stdout, level: logLevel },\n { stream: new OtelDestination(), level: 'debug' },\n ];\n\n globals[LOGGER_KEY] = pino(\n { level: logLevel, serializers: { error: pino.stdSerializers.err } },\n multistream(streams),\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAyB;AAEzB,kBAAkC;AAClC,yBAAoC;AACpC,iCAA+C;AAW/C,MAAM,aAAa,OAAO,IAAI,wBAAwB;AACtD,MAAM,qBAAqB,OAAO,IAAI,+BAA+B;AACrE,MAAM,mBAAmB,OAAO,IAAI,6BAA6B;AAQjE,MAAM,UAAU;AAGT,MAAM,gBAAgB,MAAiC,QAAQ,kBAAkB;AAGjF,MAAM,MAAM,MAAM;AACvB,QAAM,SAAS,QAAQ,UAAU;AACjC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,UAAU,mEAAmE;AAAA,EACzF;AACA,SAAO;AACT;AAGO,MAAM,mBAAmB,CAAC,EAAE,QAAQ,MAAM,MAAqB;AACpE,UAAQ,kBAA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,MAAM,EAAE,MAAM,MAAM,CAAC;AAKtD,gBAAgB;AAChB,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAiBF,gBAAgB;AAChB,eAAO,MAAM,aAAa,QAAO,aAAa,GAAG,SAAwC,CAAC;AAE1F,gBAAgB;AAChB,eAAO,MAAM,GAAG,cAMf,CAAC;AAEF,gBAAgB;AAChB,eAAO,MAAM,gBAAgB,sBAAuB,aAAa,SAMhE,CAAC;AAqBF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAqB,MAAM,EAAE,MAAM,MAAM,CAAC;AAKtD,gBAAgB;AAChB,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAiBF,gBAAgB;AAChB,eAAO,MAAM,aAAa,QAAO,aAAa,GAAG,SAAwC,CAAC;AAE1F,gBAAgB;AAChB,eAAO,MAAM,GAAG,cAMf,CAAC;AAEF,gBAAgB;AAChB,eAAO,MAAM,gBAAgB,sBAAuB,aAAa,SAMhE,CAAC;AAqBF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,YAoB7B,CAAC"}
|
package/dist/log.js
CHANGED
|
@@ -17,7 +17,7 @@ const log = () => {
|
|
|
17
17
|
const initializeLogger = ({ pretty, level }) => {
|
|
18
18
|
globals[LOGGER_OPTIONS_KEY] = { pretty, level };
|
|
19
19
|
globals[LOGGER_KEY] = pino(
|
|
20
|
-
{ level: level || "info" },
|
|
20
|
+
{ level: level || "info", serializers: { error: pino.stdSerializers.err } },
|
|
21
21
|
pretty ? pinoPretty({ colorize: true }) : process.stdout
|
|
22
22
|
);
|
|
23
23
|
};
|
|
@@ -47,7 +47,10 @@ const enableOtelLogging = () => {
|
|
|
47
47
|
{ stream: pretty ? pinoPretty({ colorize: true }) : process.stdout, level: logLevel },
|
|
48
48
|
{ stream: new OtelDestination(), level: "debug" }
|
|
49
49
|
];
|
|
50
|
-
globals[LOGGER_KEY] = pino(
|
|
50
|
+
globals[LOGGER_KEY] = pino(
|
|
51
|
+
{ level: logLevel, serializers: { error: pino.stdSerializers.err } },
|
|
52
|
+
multistream(streams)
|
|
53
|
+
);
|
|
51
54
|
};
|
|
52
55
|
export {
|
|
53
56
|
enableOtelLogging,
|
package/dist/log.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/log.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Writable } from 'node:stream';\nimport type { DestinationStream, Logger } from 'pino';\nimport { multistream, pino } from 'pino';\nimport { build as pinoPretty } from 'pino-pretty';\nimport { type PinoLogObject, emitToOtel } from './telemetry/pino_otel_transport.js';\n\n/** @internal */\nexport type LoggerOptions = {\n pretty: boolean;\n level?: string;\n};\n\n// Use Symbol.for() + globalThis to create process-wide singletons.\n// This avoids the \"dual package hazard\". Symbol.for() returns the same Symbol\n// across all module instances, and globalThis is shared process-wide.\nconst LOGGER_KEY = Symbol.for('@livekit/agents:logger');\nconst LOGGER_OPTIONS_KEY = Symbol.for('@livekit/agents:loggerOptions');\nconst OTEL_ENABLED_KEY = Symbol.for('@livekit/agents:otelEnabled');\n\ntype GlobalState = {\n [LOGGER_KEY]?: Logger;\n [LOGGER_OPTIONS_KEY]?: LoggerOptions;\n [OTEL_ENABLED_KEY]?: boolean;\n};\n\nconst globals = globalThis as typeof globalThis & GlobalState;\n\n/** @internal */\nexport const loggerOptions = (): LoggerOptions | undefined => globals[LOGGER_OPTIONS_KEY];\n\n/** @internal */\nexport const log = () => {\n const logger = globals[LOGGER_KEY];\n if (!logger) {\n throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');\n }\n return logger;\n};\n\n/** @internal */\nexport const initializeLogger = ({ pretty, level }: LoggerOptions) => {\n globals[LOGGER_OPTIONS_KEY] = { pretty, level };\n globals[LOGGER_KEY] = pino(\n { level: level || 'info' },\n pretty ? pinoPretty({ colorize: true }) : process.stdout,\n );\n};\n\n/**\n * Custom Pino destination that parses JSON logs and emits to OTEL.\n * This receives the FULL serialized log including msg, level, time, etc.\n */\nclass OtelDestination extends Writable {\n _write(chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {\n try {\n const line = chunk.toString().trim();\n if (line) {\n const logObj = JSON.parse(line) as PinoLogObject;\n emitToOtel(logObj);\n }\n } catch {\n // Ignore parse errors (e.g., non-JSON lines)\n }\n callback();\n }\n}\n\n/**\n * Enable OTEL logging by reconfiguring the logger with multistream.\n * Uses a custom destination that receives full JSON logs (with msg, level, time).\n *\n * @internal\n */\nexport const enableOtelLogging = () => {\n if (globals[OTEL_ENABLED_KEY] || !globals[LOGGER_KEY]) {\n console.warn('OTEL logging already enabled or logger not initialized');\n return;\n }\n globals[OTEL_ENABLED_KEY] = true;\n\n const opts = globals[LOGGER_OPTIONS_KEY]!;\n const { pretty, level } = opts;\n\n const logLevel = level || 'info';\n const streams: { stream: DestinationStream; level: string }[] = [\n { stream: pretty ? pinoPretty({ colorize: true }) : process.stdout, level: logLevel },\n { stream: new OtelDestination(), level: 'debug' },\n ];\n\n globals[LOGGER_KEY] = pino({ level: logLevel }
|
|
1
|
+
{"version":3,"sources":["../src/log.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Writable } from 'node:stream';\nimport type { DestinationStream, Logger } from 'pino';\nimport { multistream, pino } from 'pino';\nimport { build as pinoPretty } from 'pino-pretty';\nimport { type PinoLogObject, emitToOtel } from './telemetry/pino_otel_transport.js';\n\n/** @internal */\nexport type LoggerOptions = {\n pretty: boolean;\n level?: string;\n};\n\n// Use Symbol.for() + globalThis to create process-wide singletons.\n// This avoids the \"dual package hazard\". Symbol.for() returns the same Symbol\n// across all module instances, and globalThis is shared process-wide.\nconst LOGGER_KEY = Symbol.for('@livekit/agents:logger');\nconst LOGGER_OPTIONS_KEY = Symbol.for('@livekit/agents:loggerOptions');\nconst OTEL_ENABLED_KEY = Symbol.for('@livekit/agents:otelEnabled');\n\ntype GlobalState = {\n [LOGGER_KEY]?: Logger;\n [LOGGER_OPTIONS_KEY]?: LoggerOptions;\n [OTEL_ENABLED_KEY]?: boolean;\n};\n\nconst globals = globalThis as typeof globalThis & GlobalState;\n\n/** @internal */\nexport const loggerOptions = (): LoggerOptions | undefined => globals[LOGGER_OPTIONS_KEY];\n\n/** @internal */\nexport const log = () => {\n const logger = globals[LOGGER_KEY];\n if (!logger) {\n throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');\n }\n return logger;\n};\n\n/** @internal */\nexport const initializeLogger = ({ pretty, level }: LoggerOptions) => {\n globals[LOGGER_OPTIONS_KEY] = { pretty, level };\n globals[LOGGER_KEY] = pino(\n { level: level || 'info', serializers: { error: pino.stdSerializers.err } },\n pretty ? pinoPretty({ colorize: true }) : process.stdout,\n );\n};\n\n/**\n * Custom Pino destination that parses JSON logs and emits to OTEL.\n * This receives the FULL serialized log including msg, level, time, etc.\n */\nclass OtelDestination extends Writable {\n _write(chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {\n try {\n const line = chunk.toString().trim();\n if (line) {\n const logObj = JSON.parse(line) as PinoLogObject;\n emitToOtel(logObj);\n }\n } catch {\n // Ignore parse errors (e.g., non-JSON lines)\n }\n callback();\n }\n}\n\n/**\n * Enable OTEL logging by reconfiguring the logger with multistream.\n * Uses a custom destination that receives full JSON logs (with msg, level, time).\n *\n * @internal\n */\nexport const enableOtelLogging = () => {\n if (globals[OTEL_ENABLED_KEY] || !globals[LOGGER_KEY]) {\n console.warn('OTEL logging already enabled or logger not initialized');\n return;\n }\n globals[OTEL_ENABLED_KEY] = true;\n\n const opts = globals[LOGGER_OPTIONS_KEY]!;\n const { pretty, level } = opts;\n\n const logLevel = level || 'info';\n const streams: { stream: DestinationStream; level: string }[] = [\n { stream: pretty ? pinoPretty({ colorize: true }) : process.stdout, level: logLevel },\n { stream: new OtelDestination(), level: 'debug' },\n ];\n\n globals[LOGGER_KEY] = pino(\n { level: logLevel, serializers: { error: pino.stdSerializers.err } },\n multistream(streams),\n );\n};\n"],"mappings":"AAGA,SAAS,gBAAgB;AAEzB,SAAS,aAAa,YAAY;AAClC,SAAS,SAAS,kBAAkB;AACpC,SAA6B,kBAAkB;AAW/C,MAAM,aAAa,OAAO,IAAI,wBAAwB;AACtD,MAAM,qBAAqB,OAAO,IAAI,+BAA+B;AACrE,MAAM,mBAAmB,OAAO,IAAI,6BAA6B;AAQjE,MAAM,UAAU;AAGT,MAAM,gBAAgB,MAAiC,QAAQ,kBAAkB;AAGjF,MAAM,MAAM,MAAM;AACvB,QAAM,SAAS,QAAQ,UAAU;AACjC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,UAAU,mEAAmE;AAAA,EACzF;AACA,SAAO;AACT;AAGO,MAAM,mBAAmB,CAAC,EAAE,QAAQ,MAAM,MAAqB;AACpE,UAAQ,kBAAkB,IAAI,EAAE,QAAQ,MAAM;AAC9C,UAAQ,UAAU,IAAI;AAAA,IACpB,EAAE,OAAO,SAAS,QAAQ,aAAa,EAAE,OAAO,KAAK,eAAe,IAAI,EAAE;AAAA,IAC1E,SAAS,WAAW,EAAE,UAAU,KAAK,CAAC,IAAI,QAAQ;AAAA,EACpD;AACF;AAMA,MAAM,wBAAwB,SAAS;AAAA,EACrC,OAAO,OAAe,WAAmB,UAAgD;AACvF,QAAI;AACF,YAAM,OAAO,MAAM,SAAS,EAAE,KAAK;AACnC,UAAI,MAAM;AACR,cAAM,SAAS,KAAK,MAAM,IAAI;AAC9B,mBAAW,MAAM;AAAA,MACnB;AAAA,IACF,QAAQ;AAAA,IAER;AACA,aAAS;AAAA,EACX;AACF;AAQO,MAAM,oBAAoB,MAAM;AACrC,MAAI,QAAQ,gBAAgB,KAAK,CAAC,QAAQ,UAAU,GAAG;AACrD,YAAQ,KAAK,wDAAwD;AACrE;AAAA,EACF;AACA,UAAQ,gBAAgB,IAAI;AAE5B,QAAM,OAAO,QAAQ,kBAAkB;AACvC,QAAM,EAAE,QAAQ,MAAM,IAAI;AAE1B,QAAM,WAAW,SAAS;AAC1B,QAAM,UAA0D;AAAA,IAC9D,EAAE,QAAQ,SAAS,WAAW,EAAE,UAAU,KAAK,CAAC,IAAI,QAAQ,QAAQ,OAAO,SAAS;AAAA,IACpF,EAAE,QAAQ,IAAI,gBAAgB,GAAG,OAAO,QAAQ;AAAA,EAClD;AAEA,UAAQ,UAAU,IAAI;AAAA,IACpB,EAAE,OAAO,UAAU,aAAa,EAAE,OAAO,KAAK,eAAe,IAAI,EAAE;AAAA,IACnE,YAAY,OAAO;AAAA,EACrB;AACF;","names":[]}
|
|
@@ -56,14 +56,15 @@ class DeferredReadableStream {
|
|
|
56
56
|
if (this.isSourceSet) {
|
|
57
57
|
throw new Error("Stream source already set");
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
this.
|
|
59
|
+
const sourceReader = source.getReader();
|
|
60
|
+
this.sourceReader = sourceReader;
|
|
61
|
+
void this.pump(sourceReader);
|
|
61
62
|
}
|
|
62
|
-
async pump() {
|
|
63
|
+
async pump(sourceReader) {
|
|
63
64
|
let sourceError;
|
|
64
65
|
try {
|
|
65
66
|
while (true) {
|
|
66
|
-
const { done, value } = await
|
|
67
|
+
const { done, value } = await sourceReader.read();
|
|
67
68
|
if (done) break;
|
|
68
69
|
await this.writer.write(value);
|
|
69
70
|
}
|
|
@@ -73,7 +74,7 @@ class DeferredReadableStream {
|
|
|
73
74
|
} finally {
|
|
74
75
|
if (sourceError) {
|
|
75
76
|
try {
|
|
76
|
-
this.writer.abort(sourceError);
|
|
77
|
+
await this.writer.abort(sourceError);
|
|
77
78
|
} catch (e) {
|
|
78
79
|
}
|
|
79
80
|
return;
|
|
@@ -95,7 +96,15 @@ class DeferredReadableStream {
|
|
|
95
96
|
if (!this.isSourceSet) {
|
|
96
97
|
return;
|
|
97
98
|
}
|
|
98
|
-
this.sourceReader
|
|
99
|
+
const sourceReader = this.sourceReader;
|
|
100
|
+
this.sourceReader = void 0;
|
|
101
|
+
try {
|
|
102
|
+
sourceReader.releaseLock();
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (!isStreamReaderReleaseError(e)) {
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
99
108
|
}
|
|
100
109
|
}
|
|
101
110
|
// Annotate the CommonJS export names for ESM import in node:
|