@livekit/agents 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/dist/index.cjs +3 -0
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/llm/index.cjs +2 -0
  8. package/dist/llm/index.cjs.map +1 -1
  9. package/dist/llm/index.d.ts +1 -1
  10. package/dist/llm/index.d.ts.map +1 -1
  11. package/dist/llm/index.js +2 -0
  12. package/dist/llm/index.js.map +1 -1
  13. package/dist/llm/llm.cjs +47 -3
  14. package/dist/llm/llm.cjs.map +1 -1
  15. package/dist/llm/llm.d.ts +15 -2
  16. package/dist/llm/llm.d.ts.map +1 -1
  17. package/dist/llm/llm.js +46 -3
  18. package/dist/llm/llm.js.map +1 -1
  19. package/dist/metrics/base.cjs +44 -0
  20. package/dist/metrics/base.cjs.map +1 -0
  21. package/dist/metrics/base.d.ts +96 -0
  22. package/dist/metrics/base.d.ts.map +1 -0
  23. package/dist/metrics/base.js +20 -0
  24. package/dist/metrics/base.js.map +1 -0
  25. package/dist/metrics/index.cjs +35 -0
  26. package/dist/metrics/index.cjs.map +1 -0
  27. package/dist/metrics/index.d.ts +5 -0
  28. package/dist/metrics/index.d.ts.map +1 -0
  29. package/dist/metrics/index.js +9 -0
  30. package/dist/metrics/index.js.map +1 -0
  31. package/dist/metrics/usage_collector.cjs +53 -0
  32. package/dist/metrics/usage_collector.cjs.map +1 -0
  33. package/dist/metrics/usage_collector.d.ts +14 -0
  34. package/dist/metrics/usage_collector.d.ts.map +1 -0
  35. package/dist/metrics/usage_collector.js +29 -0
  36. package/dist/metrics/usage_collector.js.map +1 -0
  37. package/dist/metrics/utils.cjs +104 -0
  38. package/dist/metrics/utils.cjs.map +1 -0
  39. package/dist/metrics/utils.d.ts +10 -0
  40. package/dist/metrics/utils.d.ts.map +1 -0
  41. package/dist/metrics/utils.js +73 -0
  42. package/dist/metrics/utils.js.map +1 -0
  43. package/dist/multimodal/multimodal_agent.cjs +7 -13
  44. package/dist/multimodal/multimodal_agent.cjs.map +1 -1
  45. package/dist/multimodal/multimodal_agent.d.ts +1 -4
  46. package/dist/multimodal/multimodal_agent.d.ts.map +1 -1
  47. package/dist/multimodal/multimodal_agent.js +7 -13
  48. package/dist/multimodal/multimodal_agent.js.map +1 -1
  49. package/dist/pipeline/index.cjs +2 -0
  50. package/dist/pipeline/index.cjs.map +1 -1
  51. package/dist/pipeline/index.d.ts +1 -1
  52. package/dist/pipeline/index.d.ts.map +1 -1
  53. package/dist/pipeline/index.js +3 -1
  54. package/dist/pipeline/index.js.map +1 -1
  55. package/dist/pipeline/pipeline_agent.cjs +166 -66
  56. package/dist/pipeline/pipeline_agent.cjs.map +1 -1
  57. package/dist/pipeline/pipeline_agent.d.ts +10 -4
  58. package/dist/pipeline/pipeline_agent.d.ts.map +1 -1
  59. package/dist/pipeline/pipeline_agent.js +169 -69
  60. package/dist/pipeline/pipeline_agent.js.map +1 -1
  61. package/dist/pipeline/speech_handle.cjs +49 -1
  62. package/dist/pipeline/speech_handle.cjs.map +1 -1
  63. package/dist/pipeline/speech_handle.d.ts +12 -2
  64. package/dist/pipeline/speech_handle.d.ts.map +1 -1
  65. package/dist/pipeline/speech_handle.js +50 -2
  66. package/dist/pipeline/speech_handle.js.map +1 -1
  67. package/dist/stt/index.cjs.map +1 -1
  68. package/dist/stt/index.d.ts +1 -1
  69. package/dist/stt/index.d.ts.map +1 -1
  70. package/dist/stt/index.js.map +1 -1
  71. package/dist/stt/stream_adapter.cjs +15 -5
  72. package/dist/stt/stream_adapter.cjs.map +1 -1
  73. package/dist/stt/stream_adapter.d.ts +4 -1
  74. package/dist/stt/stream_adapter.d.ts.map +1 -1
  75. package/dist/stt/stream_adapter.js +15 -5
  76. package/dist/stt/stream_adapter.js.map +1 -1
  77. package/dist/stt/stt.cjs +46 -2
  78. package/dist/stt/stt.cjs.map +1 -1
  79. package/dist/stt/stt.d.ts +25 -3
  80. package/dist/stt/stt.d.ts.map +1 -1
  81. package/dist/stt/stt.js +46 -2
  82. package/dist/stt/stt.js.map +1 -1
  83. package/dist/tts/index.cjs +4 -2
  84. package/dist/tts/index.cjs.map +1 -1
  85. package/dist/tts/index.d.ts +1 -1
  86. package/dist/tts/index.d.ts.map +1 -1
  87. package/dist/tts/index.js +3 -1
  88. package/dist/tts/index.js.map +1 -1
  89. package/dist/tts/stream_adapter.cjs +14 -3
  90. package/dist/tts/stream_adapter.cjs.map +1 -1
  91. package/dist/tts/stream_adapter.d.ts +3 -0
  92. package/dist/tts/stream_adapter.d.ts.map +1 -1
  93. package/dist/tts/stream_adapter.js +15 -4
  94. package/dist/tts/stream_adapter.js.map +1 -1
  95. package/dist/tts/tts.cjs +109 -6
  96. package/dist/tts/tts.cjs.map +1 -1
  97. package/dist/tts/tts.d.ts +24 -1
  98. package/dist/tts/tts.d.ts.map +1 -1
  99. package/dist/tts/tts.js +107 -5
  100. package/dist/tts/tts.js.map +1 -1
  101. package/dist/vad.cjs +43 -2
  102. package/dist/vad.cjs.map +1 -1
  103. package/dist/vad.d.ts +21 -4
  104. package/dist/vad.d.ts.map +1 -1
  105. package/dist/vad.js +43 -2
  106. package/dist/vad.js.map +1 -1
  107. package/package.json +1 -1
  108. package/src/index.ts +2 -1
  109. package/src/llm/index.ts +2 -0
  110. package/src/llm/llm.ts +55 -3
  111. package/src/metrics/base.ts +127 -0
  112. package/src/metrics/index.ts +20 -0
  113. package/src/metrics/usage_collector.ts +40 -0
  114. package/src/metrics/utils.ts +100 -0
  115. package/src/multimodal/multimodal_agent.ts +12 -17
  116. package/src/pipeline/index.ts +1 -1
  117. package/src/pipeline/pipeline_agent.ts +206 -87
  118. package/src/pipeline/speech_handle.ts +67 -2
  119. package/src/stt/index.ts +2 -0
  120. package/src/stt/stream_adapter.ts +17 -5
  121. package/src/stt/stt.ts +67 -3
  122. package/src/tts/index.ts +2 -0
  123. package/src/tts/stream_adapter.ts +17 -4
  124. package/src/tts/tts.ts +127 -4
  125. package/src/vad.ts +61 -4
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ var src_exports = {};
31
31
  __export(src_exports, {
32
32
  cli: () => cli,
33
33
  llm: () => llm,
34
+ metrics: () => metrics,
34
35
  multimodal: () => multimodal,
35
36
  pipeline: () => pipeline,
36
37
  stt: () => stt,
@@ -40,6 +41,7 @@ __export(src_exports, {
40
41
  module.exports = __toCommonJS(src_exports);
41
42
  var cli = __toESM(require("./cli.cjs"), 1);
42
43
  var llm = __toESM(require("./llm/index.cjs"), 1);
44
+ var metrics = __toESM(require("./metrics/index.cjs"), 1);
43
45
  var multimodal = __toESM(require("./multimodal/index.cjs"), 1);
44
46
  var pipeline = __toESM(require("./pipeline/index.cjs"), 1);
45
47
  var stt = __toESM(require("./stt/index.cjs"), 1);
@@ -59,6 +61,7 @@ __reExport(src_exports, require("./transcription.cjs"), module.exports);
59
61
  0 && (module.exports = {
60
62
  cli,
61
63
  llm,
64
+ metrics,
62
65
  multimodal,
63
66
  pipeline,
64
67
  stt,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LiveKit Agents is a framework for building realtime programmable participants that run on\n * servers.\n *\n * @see {@link https://docs.livekit.io/agents/overview | LiveKit Agents documentation}\n * @packageDocumentation\n */\nimport * as cli from './cli.js';\nimport * as llm from './llm/index.js';\nimport * as multimodal from './multimodal/index.js';\nimport * as pipeline from './pipeline/index.js';\nimport * as stt from './stt/index.js';\nimport * as tokenize from './tokenize/index.js';\nimport * as tts from './tts/index.js';\n\nexport * from './vad.js';\nexport * from './plugin.js';\nexport * from './version.js';\nexport * from './job.js';\nexport * from './worker.js';\nexport * from './utils.js';\nexport * from './log.js';\nexport * from './generator.js';\nexport * from './audio.js';\nexport * from './transcription.js';\n\nexport { cli, stt, tts, llm, pipeline, multimodal, tokenize };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,UAAqB;AACrB,UAAqB;AACrB,iBAA4B;AAC5B,eAA0B;AAC1B,UAAqB;AACrB,eAA0B;AAC1B,UAAqB;AAErB,wBAAc,qBAnBd;AAoBA,wBAAc,wBApBd;AAqBA,wBAAc,yBArBd;AAsBA,wBAAc,qBAtBd;AAuBA,wBAAc,wBAvBd;AAwBA,wBAAc,uBAxBd;AAyBA,wBAAc,qBAzBd;AA0BA,wBAAc,2BA1Bd;AA2BA,wBAAc,uBA3Bd;AA4BA,wBAAc,+BA5Bd;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LiveKit Agents is a framework for building realtime programmable participants that run on\n * servers.\n *\n * @see {@link https://docs.livekit.io/agents/overview | LiveKit Agents documentation}\n * @packageDocumentation\n */\nimport * as cli from './cli.js';\nimport * as llm from './llm/index.js';\nimport * as metrics from './metrics/index.js';\nimport * as multimodal from './multimodal/index.js';\nimport * as pipeline from './pipeline/index.js';\nimport * as stt from './stt/index.js';\nimport * as tokenize from './tokenize/index.js';\nimport * as tts from './tts/index.js';\n\nexport * from './vad.js';\nexport * from './plugin.js';\nexport * from './version.js';\nexport * from './job.js';\nexport * from './worker.js';\nexport * from './utils.js';\nexport * from './log.js';\nexport * from './generator.js';\nexport * from './audio.js';\nexport * from './transcription.js';\n\nexport { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,UAAqB;AACrB,UAAqB;AACrB,cAAyB;AACzB,iBAA4B;AAC5B,eAA0B;AAC1B,UAAqB;AACrB,eAA0B;AAC1B,UAAqB;AAErB,wBAAc,qBApBd;AAqBA,wBAAc,wBArBd;AAsBA,wBAAc,yBAtBd;AAuBA,wBAAc,qBAvBd;AAwBA,wBAAc,wBAxBd;AAyBA,wBAAc,uBAzBd;AA0BA,wBAAc,qBA1Bd;AA2BA,wBAAc,2BA3Bd;AA4BA,wBAAc,uBA5Bd;AA6BA,wBAAc,+BA7Bd;","names":[]}
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import * as cli from './cli.js';
9
9
  import * as llm from './llm/index.js';
10
+ import * as metrics from './metrics/index.js';
10
11
  import * as multimodal from './multimodal/index.js';
11
12
  import * as pipeline from './pipeline/index.js';
12
13
  import * as stt from './stt/index.js';
@@ -22,5 +23,5 @@ export * from './log.js';
22
23
  export * from './generator.js';
23
24
  export * from './audio.js';
24
25
  export * from './transcription.js';
25
- export { cli, stt, tts, llm, pipeline, multimodal, tokenize };
26
+ export { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics };
26
27
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AAEtC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AAEtC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as cli from "./cli.js";
2
2
  import * as llm from "./llm/index.js";
3
+ import * as metrics from "./metrics/index.js";
3
4
  import * as multimodal from "./multimodal/index.js";
4
5
  import * as pipeline from "./pipeline/index.js";
5
6
  import * as stt from "./stt/index.js";
@@ -18,6 +19,7 @@ export * from "./transcription.js";
18
19
  export {
19
20
  cli,
20
21
  llm,
22
+ metrics,
21
23
  multimodal,
22
24
  pipeline,
23
25
  stt,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LiveKit Agents is a framework for building realtime programmable participants that run on\n * servers.\n *\n * @see {@link https://docs.livekit.io/agents/overview | LiveKit Agents documentation}\n * @packageDocumentation\n */\nimport * as cli from './cli.js';\nimport * as llm from './llm/index.js';\nimport * as multimodal from './multimodal/index.js';\nimport * as pipeline from './pipeline/index.js';\nimport * as stt from './stt/index.js';\nimport * as tokenize from './tokenize/index.js';\nimport * as tts from './tts/index.js';\n\nexport * from './vad.js';\nexport * from './plugin.js';\nexport * from './version.js';\nexport * from './job.js';\nexport * from './worker.js';\nexport * from './utils.js';\nexport * from './log.js';\nexport * from './generator.js';\nexport * from './audio.js';\nexport * from './transcription.js';\n\nexport { cli, stt, tts, llm, pipeline, multimodal, tokenize };\n"],"mappings":"AAWA,YAAY,SAAS;AACrB,YAAY,SAAS;AACrB,YAAY,gBAAgB;AAC5B,YAAY,cAAc;AAC1B,YAAY,SAAS;AACrB,YAAY,cAAc;AAC1B,YAAY,SAAS;AAErB,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LiveKit Agents is a framework for building realtime programmable participants that run on\n * servers.\n *\n * @see {@link https://docs.livekit.io/agents/overview | LiveKit Agents documentation}\n * @packageDocumentation\n */\nimport * as cli from './cli.js';\nimport * as llm from './llm/index.js';\nimport * as metrics from './metrics/index.js';\nimport * as multimodal from './multimodal/index.js';\nimport * as pipeline from './pipeline/index.js';\nimport * as stt from './stt/index.js';\nimport * as tokenize from './tokenize/index.js';\nimport * as tts from './tts/index.js';\n\nexport * from './vad.js';\nexport * from './plugin.js';\nexport * from './version.js';\nexport * from './job.js';\nexport * from './worker.js';\nexport * from './utils.js';\nexport * from './log.js';\nexport * from './generator.js';\nexport * from './audio.js';\nexport * from './transcription.js';\n\nexport { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics };\n"],"mappings":"AAWA,YAAY,SAAS;AACrB,YAAY,SAAS;AACrB,YAAY,aAAa;AACzB,YAAY,gBAAgB;AAC5B,YAAY,cAAc;AAC1B,YAAY,SAAS;AACrB,YAAY,cAAc;AAC1B,YAAY,SAAS;AAErB,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
@@ -22,6 +22,7 @@ __export(llm_exports, {
22
22
  ChatMessage: () => import_chat_context.ChatMessage,
23
23
  ChatRole: () => import_chat_context.ChatRole,
24
24
  LLM: () => import_llm.LLM,
25
+ LLMEvent: () => import_llm.LLMEvent,
25
26
  LLMStream: () => import_llm.LLMStream,
26
27
  oaiBuildFunctionInfo: () => import_function_context.oaiBuildFunctionInfo,
27
28
  oaiParams: () => import_function_context.oaiParams
@@ -36,6 +37,7 @@ var import_llm = require("./llm.cjs");
36
37
  ChatMessage,
37
38
  ChatRole,
38
39
  LLM,
40
+ LLMEvent,
39
41
  LLMStream,
40
42
  oaiBuildFunctionInfo,
41
43
  oaiParams
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/llm/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nexport {\n type CallableFunction,\n type FunctionCallInfo,\n type CallableFunctionResult,\n type FunctionContext,\n type inferParameters,\n oaiParams,\n oaiBuildFunctionInfo,\n} from './function_context.js';\n\nexport {\n type ChatImage,\n type ChatAudio,\n type ChatContent,\n ChatRole,\n ChatMessage,\n ChatContext,\n} from './chat_context.js';\n\nexport {\n type ChoiceDelta,\n type CompletionUsage,\n type Choice,\n type ChatChunk,\n LLM,\n LLMStream,\n} from './llm.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAQO;AAEP,0BAOO;AAEP,iBAOO;","names":[]}
1
+ {"version":3,"sources":["../../src/llm/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nexport {\n type CallableFunction,\n type FunctionCallInfo,\n type CallableFunctionResult,\n type FunctionContext,\n type inferParameters,\n oaiParams,\n oaiBuildFunctionInfo,\n} from './function_context.js';\n\nexport {\n type ChatImage,\n type ChatAudio,\n type ChatContent,\n ChatRole,\n ChatMessage,\n ChatContext,\n} from './chat_context.js';\n\nexport {\n type ChoiceDelta,\n type CompletionUsage,\n type Choice,\n type ChatChunk,\n type LLMCallbacks,\n LLMEvent,\n LLM,\n LLMStream,\n} from './llm.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAQO;AAEP,0BAOO;AAEP,iBASO;","names":[]}
@@ -1,4 +1,4 @@
1
1
  export { type CallableFunction, type FunctionCallInfo, type CallableFunctionResult, type FunctionContext, type inferParameters, oaiParams, oaiBuildFunctionInfo, } from './function_context.js';
2
2
  export { type ChatImage, type ChatAudio, type ChatContent, ChatRole, ChatMessage, ChatContext, } from './chat_context.js';
3
- export { type ChoiceDelta, type CompletionUsage, type Choice, type ChatChunk, LLM, LLMStream, } from './llm.js';
3
+ export { type ChoiceDelta, type CompletionUsage, type Choice, type ChatChunk, type LLMCallbacks, LLMEvent, LLM, LLMStream, } from './llm.js';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAC3B,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,SAAS,EACT,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,QAAQ,EACR,WAAW,EACX,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,SAAS,EACd,GAAG,EACH,SAAS,GACV,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAC3B,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,SAAS,EACT,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,QAAQ,EACR,WAAW,EACX,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,QAAQ,EACR,GAAG,EACH,SAAS,GACV,MAAM,UAAU,CAAC"}
package/dist/llm/index.js CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  ChatContext
9
9
  } from "./chat_context.js";
10
10
  import {
11
+ LLMEvent,
11
12
  LLM,
12
13
  LLMStream
13
14
  } from "./llm.js";
@@ -16,6 +17,7 @@ export {
16
17
  ChatMessage,
17
18
  ChatRole,
18
19
  LLM,
20
+ LLMEvent,
19
21
  LLMStream,
20
22
  oaiBuildFunctionInfo,
21
23
  oaiParams
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/llm/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nexport {\n type CallableFunction,\n type FunctionCallInfo,\n type CallableFunctionResult,\n type FunctionContext,\n type inferParameters,\n oaiParams,\n oaiBuildFunctionInfo,\n} from './function_context.js';\n\nexport {\n type ChatImage,\n type ChatAudio,\n type ChatContent,\n ChatRole,\n ChatMessage,\n ChatContext,\n} from './chat_context.js';\n\nexport {\n type ChoiceDelta,\n type CompletionUsage,\n type Choice,\n type ChatChunk,\n LLM,\n LLMStream,\n} from './llm.js';\n"],"mappings":"AAGA;AAAA,EAME;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EAKE;AAAA,EACA;AAAA,OACK;","names":[]}
1
+ {"version":3,"sources":["../../src/llm/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nexport {\n type CallableFunction,\n type FunctionCallInfo,\n type CallableFunctionResult,\n type FunctionContext,\n type inferParameters,\n oaiParams,\n oaiBuildFunctionInfo,\n} from './function_context.js';\n\nexport {\n type ChatImage,\n type ChatAudio,\n type ChatContent,\n ChatRole,\n ChatMessage,\n ChatContext,\n} from './chat_context.js';\n\nexport {\n type ChoiceDelta,\n type CompletionUsage,\n type Choice,\n type ChatChunk,\n type LLMCallbacks,\n LLMEvent,\n LLM,\n LLMStream,\n} from './llm.js';\n"],"mappings":"AAGA;AAAA,EAME;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EAME;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
package/dist/llm/llm.cjs CHANGED
@@ -19,21 +19,63 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var llm_exports = {};
20
20
  __export(llm_exports, {
21
21
  LLM: () => LLM,
22
+ LLMEvent: () => LLMEvent,
22
23
  LLMStream: () => LLMStream
23
24
  });
24
25
  module.exports = __toCommonJS(llm_exports);
26
+ var import_node_events = require("node:events");
25
27
  var import_utils = require("../utils.cjs");
26
- class LLM {
28
+ var LLMEvent = /* @__PURE__ */ ((LLMEvent2) => {
29
+ LLMEvent2[LLMEvent2["METRICS_COLLECTED"] = 0] = "METRICS_COLLECTED";
30
+ return LLMEvent2;
31
+ })(LLMEvent || {});
32
+ class LLM extends import_node_events.EventEmitter {
27
33
  }
28
34
  class LLMStream {
35
+ output = new import_utils.AsyncIterableQueue();
29
36
  queue = new import_utils.AsyncIterableQueue();
30
37
  closed = false;
31
38
  _functionCalls = [];
39
+ #llm;
32
40
  #chatCtx;
33
41
  #fncCtx;
34
- constructor(chatCtx, fncCtx) {
42
+ constructor(llm, chatCtx, fncCtx) {
43
+ this.#llm = llm;
35
44
  this.#chatCtx = chatCtx;
36
45
  this.#fncCtx = fncCtx;
46
+ this.monitorMetrics();
47
+ }
48
+ async monitorMetrics() {
49
+ const startTime = process.hrtime.bigint();
50
+ let ttft;
51
+ let requestId = "";
52
+ let usage;
53
+ for await (const ev of this.queue) {
54
+ this.output.put(ev);
55
+ requestId = ev.requestId;
56
+ if (!ttft) {
57
+ ttft = process.hrtime.bigint() - startTime;
58
+ }
59
+ if (ev.usage) {
60
+ usage = ev.usage;
61
+ }
62
+ }
63
+ this.output.close();
64
+ const duration = process.hrtime.bigint() - startTime;
65
+ const metrics = {
66
+ timestamp: Date.now(),
67
+ requestId,
68
+ ttft: Math.trunc(Number(ttft / BigInt(1e6))),
69
+ duration: Math.trunc(Number(duration / BigInt(1e6))),
70
+ cancelled: false,
71
+ // XXX(nbsp)
72
+ label: this.label,
73
+ completionTokens: (usage == null ? void 0 : usage.completionTokens) || 0,
74
+ promptTokens: (usage == null ? void 0 : usage.promptTokens) || 0,
75
+ totalTokens: (usage == null ? void 0 : usage.totalTokens) || 0,
76
+ tokensPerSecond: ((usage == null ? void 0 : usage.completionTokens) || 0) / Math.trunc(Number(duration / BigInt(1e9)))
77
+ };
78
+ this.#llm.emit(0 /* METRICS_COLLECTED */, metrics);
37
79
  }
38
80
  /** List of called functions from this stream. */
39
81
  get functionCalls() {
@@ -58,9 +100,10 @@ class LLMStream {
58
100
  return this._functionCalls;
59
101
  }
60
102
  next() {
61
- return this.queue.next();
103
+ return this.output.next();
62
104
  }
63
105
  close() {
106
+ this.output.close();
64
107
  this.queue.close();
65
108
  this.closed = true;
66
109
  }
@@ -71,6 +114,7 @@ class LLMStream {
71
114
  // Annotate the CommonJS export names for ESM import in node:
72
115
  0 && (module.exports = {
73
116
  LLM,
117
+ LLMEvent,
74
118
  LLMStream
75
119
  });
76
120
  //# sourceMappingURL=llm.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncIterableQueue } from '../utils.js';\nimport type { ChatContext, ChatRole } from './chat_context.js';\nimport type { FunctionCallInfo, FunctionContext } from './function_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCallInfo[];\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface Choice {\n delta: ChoiceDelta;\n index: number;\n}\n\nexport interface ChatChunk {\n requestId: string;\n choices: Choice[];\n usage?: CompletionUsage;\n}\n\nexport abstract class LLM {\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n fncCtx,\n temperature,\n n,\n parallelToolCalls,\n }: {\n chatCtx: ChatContext;\n fncCtx?: FunctionContext;\n temperature?: number;\n n?: number;\n parallelToolCalls?: boolean;\n }): LLMStream;\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected _functionCalls: FunctionCallInfo[] = [];\n\n #chatCtx: ChatContext;\n #fncCtx?: FunctionContext;\n\n constructor(chatCtx: ChatContext, fncCtx?: FunctionContext) {\n this.#chatCtx = chatCtx;\n this.#fncCtx = fncCtx;\n }\n\n /** List of called functions from this stream. */\n get functionCalls(): FunctionCallInfo[] {\n return this._functionCalls;\n }\n\n /** The function context of this stream. */\n get fncCtx(): FunctionContext | undefined {\n return this.#fncCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** Execute all deferred functions of this stream concurrently. */\n executeFunctions(): FunctionCallInfo[] {\n this._functionCalls.forEach(\n (f) =>\n (f.task = f.func.execute(f.params).then(\n (result) => ({ name: f.name, toolCallId: f.toolCallId, result }),\n (error) => ({ name: f.name, toolCallId: f.toolCallId, error }),\n )),\n );\n return this._functionCalls;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.queue.next();\n }\n\n close() {\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAAmC;AA2B5B,MAAe,IAAI;AAiB1B;AAEO,MAAe,UAAsD;AAAA,EAChE,QAAQ,IAAI,gCAA8B;AAAA,EAC1C,SAAS;AAAA,EACT,iBAAqC,CAAC;AAAA,EAEhD;AAAA,EACA;AAAA,EAEA,YAAY,SAAsB,QAA0B;AAC1D,SAAK,WAAW;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGA,IAAI,gBAAoC;AACtC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,SAAsC;AACxC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,UAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,mBAAuC;AACrC,SAAK,eAAe;AAAA,MAClB,CAAC,MACE,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,EAAE;AAAA,QACjC,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,YAAY,EAAE,YAAY,OAAO;AAAA,QAC9D,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,YAAY,EAAE,YAAY,MAAM;AAAA,MAC9D;AAAA,IACJ;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAA2C;AACzC,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,QAAQ;AACN,SAAK,MAAM,MAAM;AACjB,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport { EventEmitter } from 'node:events';\nimport type { LLMMetrics } from '../metrics/base.js';\nimport { AsyncIterableQueue } from '../utils.js';\nimport type { ChatContext, ChatRole } from './chat_context.js';\nimport type { FunctionCallInfo, FunctionContext } from './function_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCallInfo[];\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface Choice {\n delta: ChoiceDelta;\n index: number;\n}\n\nexport interface ChatChunk {\n requestId: string;\n choices: Choice[];\n usage?: CompletionUsage;\n}\n\nexport enum LLMEvent {\n METRICS_COLLECTED,\n}\n\nexport type LLMCallbacks = {\n [LLMEvent.METRICS_COLLECTED]: (metrics: LLMMetrics) => void;\n};\n\nexport abstract class LLM extends (EventEmitter as new () => TypedEmitter<LLMCallbacks>) {\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n fncCtx,\n temperature,\n n,\n parallelToolCalls,\n }: {\n chatCtx: ChatContext;\n fncCtx?: FunctionContext;\n temperature?: number;\n n?: number;\n parallelToolCalls?: boolean;\n }): LLMStream;\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected output = new AsyncIterableQueue<ChatChunk>();\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected _functionCalls: FunctionCallInfo[] = [];\n abstract label: string;\n\n #llm: LLM;\n #chatCtx: ChatContext;\n #fncCtx?: FunctionContext;\n\n constructor(llm: LLM, chatCtx: ChatContext, fncCtx?: FunctionContext) {\n this.#llm = llm;\n this.#chatCtx = chatCtx;\n this.#fncCtx = fncCtx;\n this.monitorMetrics();\n }\n\n protected async monitorMetrics() {\n const startTime = process.hrtime.bigint();\n let ttft: bigint | undefined;\n let requestId = '';\n let usage: CompletionUsage | undefined;\n\n for await (const ev of this.queue) {\n this.output.put(ev);\n requestId = ev.requestId;\n if (!ttft) {\n ttft = process.hrtime.bigint() - startTime;\n }\n if (ev.usage) {\n usage = ev.usage;\n }\n }\n this.output.close();\n\n const duration = process.hrtime.bigint() - startTime;\n const metrics: LLMMetrics = {\n timestamp: Date.now(),\n requestId,\n ttft: Math.trunc(Number(ttft! / BigInt(1000000))),\n duration: Math.trunc(Number(duration / BigInt(1000000))),\n cancelled: false, // XXX(nbsp)\n label: this.label,\n completionTokens: usage?.completionTokens || 0,\n promptTokens: usage?.promptTokens || 0,\n totalTokens: usage?.totalTokens || 0,\n tokensPerSecond:\n (usage?.completionTokens || 0) / Math.trunc(Number(duration / BigInt(1000000000))),\n };\n this.#llm.emit(LLMEvent.METRICS_COLLECTED, metrics);\n }\n\n /** List of called functions from this stream. */\n get functionCalls(): FunctionCallInfo[] {\n return this._functionCalls;\n }\n\n /** The function context of this stream. */\n get fncCtx(): FunctionContext | undefined {\n return this.#fncCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** Execute all deferred functions of this stream concurrently. */\n executeFunctions(): FunctionCallInfo[] {\n this._functionCalls.forEach(\n (f) =>\n (f.task = f.func.execute(f.params).then(\n (result) => ({ name: f.name, toolCallId: f.toolCallId, result }),\n (error) => ({ name: f.name, toolCallId: f.toolCallId, error }),\n )),\n );\n return this._functionCalls;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.output.next();\n }\n\n close() {\n this.output.close();\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,yBAA6B;AAE7B,mBAAmC;AA2B5B,IAAK,WAAL,kBAAKA,cAAL;AACL,EAAAA,oBAAA;AADU,SAAAA;AAAA,GAAA;AAQL,MAAe,YAAa,gCAAsD;AAiBzF;AAEO,MAAe,UAAsD;AAAA,EAChE,SAAS,IAAI,gCAA8B;AAAA,EAC3C,QAAQ,IAAI,gCAA8B;AAAA,EAC1C,SAAS;AAAA,EACT,iBAAqC,CAAC;AAAA,EAGhD;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,KAAU,SAAsB,QAA0B;AACpE,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,SAAK,UAAU;AACf,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAgB,iBAAiB;AAC/B,UAAM,YAAY,QAAQ,OAAO,OAAO;AACxC,QAAI;AACJ,QAAI,YAAY;AAChB,QAAI;AAEJ,qBAAiB,MAAM,KAAK,OAAO;AACjC,WAAK,OAAO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,EAEA,OAA2C;AACzC,WAAO,KAAK,OAAO,KAAK;AAAA,EAC1B;AAAA,EAEA,QAAQ;AACN,SAAK,OAAO,MAAM;AAClB,SAAK,MAAM,MAAM;AACjB,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":["LLMEvent"]}
package/dist/llm/llm.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
2
+ import type { LLMMetrics } from '../metrics/base.js';
1
3
  import { AsyncIterableQueue } from '../utils.js';
2
4
  import type { ChatContext, ChatRole } from './chat_context.js';
3
5
  import type { FunctionCallInfo, FunctionContext } from './function_context.js';
@@ -20,7 +22,14 @@ export interface ChatChunk {
20
22
  choices: Choice[];
21
23
  usage?: CompletionUsage;
22
24
  }
23
- export declare abstract class LLM {
25
+ export declare enum LLMEvent {
26
+ METRICS_COLLECTED = 0
27
+ }
28
+ export type LLMCallbacks = {
29
+ [LLMEvent.METRICS_COLLECTED]: (metrics: LLMMetrics) => void;
30
+ };
31
+ declare const LLM_base: new () => TypedEmitter<LLMCallbacks>;
32
+ export declare abstract class LLM extends LLM_base {
24
33
  /**
25
34
  * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.
26
35
  */
@@ -34,10 +43,13 @@ export declare abstract class LLM {
34
43
  }
35
44
  export declare abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {
36
45
  #private;
46
+ protected output: AsyncIterableQueue<ChatChunk>;
37
47
  protected queue: AsyncIterableQueue<ChatChunk>;
38
48
  protected closed: boolean;
39
49
  protected _functionCalls: FunctionCallInfo[];
40
- constructor(chatCtx: ChatContext, fncCtx?: FunctionContext);
50
+ abstract label: string;
51
+ constructor(llm: LLM, chatCtx: ChatContext, fncCtx?: FunctionContext);
52
+ protected monitorMetrics(): Promise<void>;
41
53
  /** List of called functions from this stream. */
42
54
  get functionCalls(): FunctionCallInfo[];
43
55
  /** The function context of this stream. */
@@ -50,4 +62,5 @@ export declare abstract class LLMStream implements AsyncIterableIterator<ChatChu
50
62
  close(): void;
51
63
  [Symbol.asyncIterator](): LLMStream;
52
64
  }
65
+ export {};
53
66
  //# sourceMappingURL=llm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../../src/llm/llm.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE/E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,gBAAgB,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,WAAW,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,SAAS;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAED,8BAAsB,GAAG;IACvB;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,EACZ,OAAO,EACP,MAAM,EACN,WAAW,EACX,CAAC,EACD,iBAAiB,GAClB,EAAE;QACD,OAAO,EAAE,WAAW,CAAC;QACrB,MAAM,CAAC,EAAE,eAAe,CAAC;QACzB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,CAAC,CAAC,EAAE,MAAM,CAAC;QACX,iBAAiB,CAAC,EAAE,OAA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AAM,IAAI,eAAe,GAAG,SAAS,CAExC;IAED,+CAA+C;IAC/C,IAAI,OAAO,IAAI,WAAW,CAEzB;IAED,kEAAkE;IAClE,gBAAgB,IAAI,gBAAgB,EAAE;IAWtC,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IAI1C,KAAK;IAML,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,SAAS;CAGpC"}
package/dist/llm/llm.js CHANGED
@@ -1,15 +1,56 @@
1
+ import { EventEmitter } from "node:events";
1
2
  import { AsyncIterableQueue } from "../utils.js";
2
- class LLM {
3
+ var LLMEvent = /* @__PURE__ */ ((LLMEvent2) => {
4
+ LLMEvent2[LLMEvent2["METRICS_COLLECTED"] = 0] = "METRICS_COLLECTED";
5
+ return LLMEvent2;
6
+ })(LLMEvent || {});
7
+ class LLM extends EventEmitter {
3
8
  }
4
9
  class LLMStream {
10
+ output = new AsyncIterableQueue();
5
11
  queue = new AsyncIterableQueue();
6
12
  closed = false;
7
13
  _functionCalls = [];
14
+ #llm;
8
15
  #chatCtx;
9
16
  #fncCtx;
10
- constructor(chatCtx, fncCtx) {
17
+ constructor(llm, chatCtx, fncCtx) {
18
+ this.#llm = llm;
11
19
  this.#chatCtx = chatCtx;
12
20
  this.#fncCtx = fncCtx;
21
+ this.monitorMetrics();
22
+ }
23
+ async monitorMetrics() {
24
+ const startTime = process.hrtime.bigint();
25
+ let ttft;
26
+ let requestId = "";
27
+ let usage;
28
+ for await (const ev of this.queue) {
29
+ this.output.put(ev);
30
+ requestId = ev.requestId;
31
+ if (!ttft) {
32
+ ttft = process.hrtime.bigint() - startTime;
33
+ }
34
+ if (ev.usage) {
35
+ usage = ev.usage;
36
+ }
37
+ }
38
+ this.output.close();
39
+ const duration = process.hrtime.bigint() - startTime;
40
+ const metrics = {
41
+ timestamp: Date.now(),
42
+ requestId,
43
+ ttft: Math.trunc(Number(ttft / BigInt(1e6))),
44
+ duration: Math.trunc(Number(duration / BigInt(1e6))),
45
+ cancelled: false,
46
+ // XXX(nbsp)
47
+ label: this.label,
48
+ completionTokens: (usage == null ? void 0 : usage.completionTokens) || 0,
49
+ promptTokens: (usage == null ? void 0 : usage.promptTokens) || 0,
50
+ totalTokens: (usage == null ? void 0 : usage.totalTokens) || 0,
51
+ tokensPerSecond: ((usage == null ? void 0 : usage.completionTokens) || 0) / Math.trunc(Number(duration / BigInt(1e9)))
52
+ };
53
+ this.#llm.emit(0 /* METRICS_COLLECTED */, metrics);
13
54
  }
14
55
  /** List of called functions from this stream. */
15
56
  get functionCalls() {
@@ -34,9 +75,10 @@ class LLMStream {
34
75
  return this._functionCalls;
35
76
  }
36
77
  next() {
37
- return this.queue.next();
78
+ return this.output.next();
38
79
  }
39
80
  close() {
81
+ this.output.close();
40
82
  this.queue.close();
41
83
  this.closed = true;
42
84
  }
@@ -46,6 +88,7 @@ class LLMStream {
46
88
  }
47
89
  export {
48
90
  LLM,
91
+ LLMEvent,
49
92
  LLMStream
50
93
  };
51
94
  //# sourceMappingURL=llm.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncIterableQueue } from '../utils.js';\nimport type { ChatContext, ChatRole } from './chat_context.js';\nimport type { FunctionCallInfo, FunctionContext } from './function_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCallInfo[];\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface Choice {\n delta: ChoiceDelta;\n index: number;\n}\n\nexport interface ChatChunk {\n requestId: string;\n choices: Choice[];\n usage?: CompletionUsage;\n}\n\nexport abstract class LLM {\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n fncCtx,\n temperature,\n n,\n parallelToolCalls,\n }: {\n chatCtx: ChatContext;\n fncCtx?: FunctionContext;\n temperature?: number;\n n?: number;\n parallelToolCalls?: boolean;\n }): LLMStream;\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected _functionCalls: FunctionCallInfo[] = [];\n\n #chatCtx: ChatContext;\n #fncCtx?: FunctionContext;\n\n constructor(chatCtx: ChatContext, fncCtx?: FunctionContext) {\n this.#chatCtx = chatCtx;\n this.#fncCtx = fncCtx;\n }\n\n /** List of called functions from this stream. */\n get functionCalls(): FunctionCallInfo[] {\n return this._functionCalls;\n }\n\n /** The function context of this stream. */\n get fncCtx(): FunctionContext | undefined {\n return this.#fncCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** Execute all deferred functions of this stream concurrently. */\n executeFunctions(): FunctionCallInfo[] {\n this._functionCalls.forEach(\n (f) =>\n (f.task = f.func.execute(f.params).then(\n (result) => ({ name: f.name, toolCallId: f.toolCallId, result }),\n (error) => ({ name: f.name, toolCallId: f.toolCallId, error }),\n )),\n );\n return this._functionCalls;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.queue.next();\n }\n\n close() {\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":"AAGA,SAAS,0BAA0B;AA2B5B,MAAe,IAAI;AAiB1B;AAEO,MAAe,UAAsD;AAAA,EAChE,QAAQ,IAAI,mBAA8B;AAAA,EAC1C,SAAS;AAAA,EACT,iBAAqC,CAAC;AAAA,EAEhD;AAAA,EACA;AAAA,EAEA,YAAY,SAAsB,QAA0B;AAC1D,SAAK,WAAW;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGA,IAAI,gBAAoC;AACtC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,SAAsC;AACxC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,UAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,mBAAuC;AACrC,SAAK,eAAe;AAAA,MAClB,CAAC,MACE,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,EAAE;AAAA,QACjC,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,YAAY,EAAE,YAAY,OAAO;AAAA,QAC9D,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,YAAY,EAAE,YAAY,MAAM;AAAA,MAC9D;AAAA,IACJ;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAA2C;AACzC,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,QAAQ;AACN,SAAK,MAAM,MAAM;AACjB,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport { EventEmitter } from 'node:events';\nimport type { LLMMetrics } from '../metrics/base.js';\nimport { AsyncIterableQueue } from '../utils.js';\nimport type { ChatContext, ChatRole } from './chat_context.js';\nimport type { FunctionCallInfo, FunctionContext } from './function_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCallInfo[];\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface Choice {\n delta: ChoiceDelta;\n index: number;\n}\n\nexport interface ChatChunk {\n requestId: string;\n choices: Choice[];\n usage?: CompletionUsage;\n}\n\nexport enum LLMEvent {\n METRICS_COLLECTED,\n}\n\nexport type LLMCallbacks = {\n [LLMEvent.METRICS_COLLECTED]: (metrics: LLMMetrics) => void;\n};\n\nexport abstract class LLM extends (EventEmitter as new () => TypedEmitter<LLMCallbacks>) {\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n fncCtx,\n temperature,\n n,\n parallelToolCalls,\n }: {\n chatCtx: ChatContext;\n fncCtx?: FunctionContext;\n temperature?: number;\n n?: number;\n parallelToolCalls?: boolean;\n }): LLMStream;\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected output = new AsyncIterableQueue<ChatChunk>();\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected _functionCalls: FunctionCallInfo[] = [];\n abstract label: string;\n\n #llm: LLM;\n #chatCtx: ChatContext;\n #fncCtx?: FunctionContext;\n\n constructor(llm: LLM, chatCtx: ChatContext, fncCtx?: FunctionContext) {\n this.#llm = llm;\n this.#chatCtx = chatCtx;\n this.#fncCtx = fncCtx;\n this.monitorMetrics();\n }\n\n protected async monitorMetrics() {\n const startTime = process.hrtime.bigint();\n let ttft: bigint | undefined;\n let requestId = '';\n let usage: CompletionUsage | undefined;\n\n for await (const ev of this.queue) {\n this.output.put(ev);\n requestId = ev.requestId;\n if (!ttft) {\n ttft = process.hrtime.bigint() - startTime;\n }\n if (ev.usage) {\n usage = ev.usage;\n }\n }\n this.output.close();\n\n const duration = process.hrtime.bigint() - startTime;\n const metrics: LLMMetrics = {\n timestamp: Date.now(),\n requestId,\n ttft: Math.trunc(Number(ttft! / BigInt(1000000))),\n duration: Math.trunc(Number(duration / BigInt(1000000))),\n cancelled: false, // XXX(nbsp)\n label: this.label,\n completionTokens: usage?.completionTokens || 0,\n promptTokens: usage?.promptTokens || 0,\n totalTokens: usage?.totalTokens || 0,\n tokensPerSecond:\n (usage?.completionTokens || 0) / Math.trunc(Number(duration / BigInt(1000000000))),\n };\n this.#llm.emit(LLMEvent.METRICS_COLLECTED, metrics);\n }\n\n /** List of called functions from this stream. */\n get functionCalls(): FunctionCallInfo[] {\n return this._functionCalls;\n }\n\n /** The function context of this stream. */\n get fncCtx(): FunctionContext | undefined {\n return this.#fncCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** Execute all deferred functions of this stream concurrently. */\n executeFunctions(): FunctionCallInfo[] {\n this._functionCalls.forEach(\n (f) =>\n (f.task = f.func.execute(f.params).then(\n (result) => ({ name: f.name, toolCallId: f.toolCallId, result }),\n (error) => ({ name: f.name, toolCallId: f.toolCallId, error }),\n )),\n );\n return this._functionCalls;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.output.next();\n }\n\n close() {\n this.output.close();\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":"AAIA,SAAS,oBAAoB;AAE7B,SAAS,0BAA0B;AA2B5B,IAAK,WAAL,kBAAKA,cAAL;AACL,EAAAA,oBAAA;AADU,SAAAA;AAAA,GAAA;AAQL,MAAe,YAAa,aAAsD;AAiBzF;AAEO,MAAe,UAAsD;AAAA,EAChE,SAAS,IAAI,mBAA8B;AAAA,EAC3C,QAAQ,IAAI,mBAA8B;AAAA,EAC1C,SAAS;AAAA,EACT,iBAAqC,CAAC;AAAA,EAGhD;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,KAAU,SAAsB,QAA0B;AACpE,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,SAAK,UAAU;AACf,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAgB,iBAAiB;AAC/B,UAAM,YAAY,QAAQ,OAAO,OAAO;AACxC,QAAI;AACJ,QAAI,YAAY;AAChB,QAAI;AAEJ,qBAAiB,MAAM,KAAK,OAAO;AACjC,WAAK,OAAO,IAAI,EAAE;AAClB,kBAAY,GAAG;AACf,UAAI,CAAC,MAAM;AACT,eAAO,QAAQ,OAAO,OAAO,IAAI;AAAA,MACnC;AACA,UAAI,GAAG,OAAO;AACZ,gBAAQ,GAAG;AAAA,MACb;AAAA,IAC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CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":["LLMEvent"]}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var base_exports = {};
20
+ __export(base_exports, {
21
+ MultimodalLLMError: () => MultimodalLLMError
22
+ });
23
+ module.exports = __toCommonJS(base_exports);
24
+ class MultimodalLLMError extends Error {
25
+ type;
26
+ reason;
27
+ code;
28
+ constructor({
29
+ type,
30
+ reason,
31
+ code,
32
+ message
33
+ } = {}, options) {
34
+ super(message, options);
35
+ this.type = type;
36
+ this.reason = reason;
37
+ this.code = code;
38
+ }
39
+ }
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ MultimodalLLMError
43
+ });
44
+ //# sourceMappingURL=base.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/metrics/base.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\nexport interface LLMMetrics {\n requestId: string;\n timestamp: number;\n ttft: number;\n duration: number;\n label: string;\n cancelled: boolean;\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n tokensPerSecond: number;\n error?: Error;\n}\n\nexport interface STTMetrics {\n requestId: string;\n timestamp: number;\n duration: number;\n label: string;\n audioDuration: number;\n streamed: boolean;\n error?: Error;\n}\n\nexport interface TTSMetrics {\n requestId: string;\n timestamp: number;\n ttfb: number;\n duration: number;\n label: string;\n audioDuration: number;\n cancelled: boolean;\n charactersCount: number;\n streamed: boolean;\n error?: Error;\n}\n\nexport interface VADMetrics {\n timestamp: number;\n idleTime: number;\n inferenceDurationTotal: number;\n inferenceCount: number;\n label: string;\n}\n\nexport interface PipelineEOUMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n /** Timestamp of when the event was recorded */\n timestamp: number;\n /** Amount of time between the end of speech from VAD and the decision to end the user's turn */\n endOfUtteranceDelay: number;\n /**\n * Time taken to obtain the transcript after the end of the user's speech.\n *\n * @remarks\n * May be 0 if the transcript was already available.\n */\n transcriptionDelay: number;\n}\n\nexport interface PipelineLLMMetrics extends LLMMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n}\n\nexport interface PipelineTTSMetrics extends TTSMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n}\n\nexport type PipelineSTTMetrics = STTMetrics;\nexport type PipelineVADMetrics = VADMetrics;\n\nexport class MultimodalLLMError extends Error {\n type?: string;\n reason?: string;\n code?: string;\n constructor(\n {\n type,\n reason,\n code,\n message,\n }: { type?: string; reason?: string; code?: string; message?: string } = {},\n options?: ErrorOptions,\n ) {\n super(message, options);\n this.type = type;\n this.reason = reason;\n this.code = code;\n }\n}\n\nexport interface MultimodalLLMMetrics extends LLMMetrics {\n inputTokenDetails: {\n cachedTokens: number;\n textTokens: number;\n audioTokens: number;\n };\n outputTokenDetails: {\n textTokens: number;\n audioTokens: number;\n };\n}\n\nexport type AgentMetrics =\n | STTMetrics\n | LLMMetrics\n | TTSMetrics\n | VADMetrics\n | PipelineSTTMetrics\n | PipelineEOUMetrics\n | PipelineLLMMetrics\n | PipelineTTSMetrics\n | PipelineVADMetrics\n | MultimodalLLMMetrics;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAoFO,MAAM,2BAA2B,MAAM;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA,YACE;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAyE,CAAC,GAC1E,SACA;AACA,UAAM,SAAS,OAAO;AACtB,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,96 @@
1
+ export interface LLMMetrics {
2
+ requestId: string;
3
+ timestamp: number;
4
+ ttft: number;
5
+ duration: number;
6
+ label: string;
7
+ cancelled: boolean;
8
+ completionTokens: number;
9
+ promptTokens: number;
10
+ totalTokens: number;
11
+ tokensPerSecond: number;
12
+ error?: Error;
13
+ }
14
+ export interface STTMetrics {
15
+ requestId: string;
16
+ timestamp: number;
17
+ duration: number;
18
+ label: string;
19
+ audioDuration: number;
20
+ streamed: boolean;
21
+ error?: Error;
22
+ }
23
+ export interface TTSMetrics {
24
+ requestId: string;
25
+ timestamp: number;
26
+ ttfb: number;
27
+ duration: number;
28
+ label: string;
29
+ audioDuration: number;
30
+ cancelled: boolean;
31
+ charactersCount: number;
32
+ streamed: boolean;
33
+ error?: Error;
34
+ }
35
+ export interface VADMetrics {
36
+ timestamp: number;
37
+ idleTime: number;
38
+ inferenceDurationTotal: number;
39
+ inferenceCount: number;
40
+ label: string;
41
+ }
42
+ export interface PipelineEOUMetrics {
43
+ /**
44
+ * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
45
+ */
46
+ sequenceId: string;
47
+ /** Timestamp of when the event was recorded */
48
+ timestamp: number;
49
+ /** Amount of time between the end of speech from VAD and the decision to end the user's turn */
50
+ endOfUtteranceDelay: number;
51
+ /**
52
+ * Time taken to obtain the transcript after the end of the user's speech.
53
+ *
54
+ * @remarks
55
+ * May be 0 if the transcript was already available.
56
+ */
57
+ transcriptionDelay: number;
58
+ }
59
+ export interface PipelineLLMMetrics extends LLMMetrics {
60
+ /**
61
+ * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
62
+ */
63
+ sequenceId: string;
64
+ }
65
+ export interface PipelineTTSMetrics extends TTSMetrics {
66
+ /**
67
+ * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
68
+ */
69
+ sequenceId: string;
70
+ }
71
+ export type PipelineSTTMetrics = STTMetrics;
72
+ export type PipelineVADMetrics = VADMetrics;
73
+ export declare class MultimodalLLMError extends Error {
74
+ type?: string;
75
+ reason?: string;
76
+ code?: string;
77
+ constructor({ type, reason, code, message, }?: {
78
+ type?: string;
79
+ reason?: string;
80
+ code?: string;
81
+ message?: string;
82
+ }, options?: ErrorOptions);
83
+ }
84
+ export interface MultimodalLLMMetrics extends LLMMetrics {
85
+ inputTokenDetails: {
86
+ cachedTokens: number;
87
+ textTokens: number;
88
+ audioTokens: number;
89
+ };
90
+ outputTokenDetails: {
91
+ textTokens: number;
92
+ audioTokens: number;
93
+ };
94
+ }
95
+ export type AgentMetrics = STTMetrics | LLMMetrics | TTSMetrics | VADMetrics | PipelineSTTMetrics | PipelineEOUMetrics | PipelineLLMMetrics | PipelineTTSMetrics | PipelineVADMetrics | MultimodalLLMMetrics;
96
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/metrics/base.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,OAAO,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACz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IACpD;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,kBAAkB,GAAG,UAAU,CAAC;AAC5C,MAAM,MAAM,kBAAkB,GAAG,UAAU,CAAC;AAE5C,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;gBAEZ,EACE,IAAI,EACJ,MAAM,EACN,IAAI,EACJ,OAAO,GACR,GAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO,EAC3E,OAAO,CAAC,EAAE,YAAY;CAOzB;AAED,MAAM,WAAW,oBAAqB,SAAQ,UAAU;IACtD,iBAAiB,EAAE;QACjB,YAAY,EAAE,MAAM,CAAC;QACrB,UAAU,EAAE,MAAM,CAAC;QACnB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,kBAAkB,EAAE;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;CACH;AAED,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,UAAU,GACV,UAAU,GACV,UAAU,GACV,kBAAkB,GAClB,kBAAkB,GAClB,kBAAkB,GAClB,kBAAkB,GAClB,kBAAkB,GAClB,oBAAoB,CAAC"}
@@ -0,0 +1,20 @@
1
+ class MultimodalLLMError extends Error {
2
+ type;
3
+ reason;
4
+ code;
5
+ constructor({
6
+ type,
7
+ reason,
8
+ code,
9
+ message
10
+ } = {}, options) {
11
+ super(message, options);
12
+ this.type = type;
13
+ this.reason = reason;
14
+ this.code = code;
15
+ }
16
+ }
17
+ export {
18
+ MultimodalLLMError
19
+ };
20
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/metrics/base.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\nexport interface LLMMetrics {\n requestId: string;\n timestamp: number;\n ttft: number;\n duration: number;\n label: string;\n cancelled: boolean;\n completionTokens: number;\n promptTokens: number;\n totalTokens: number;\n tokensPerSecond: number;\n error?: Error;\n}\n\nexport interface STTMetrics {\n requestId: string;\n timestamp: number;\n duration: number;\n label: string;\n audioDuration: number;\n streamed: boolean;\n error?: Error;\n}\n\nexport interface TTSMetrics {\n requestId: string;\n timestamp: number;\n ttfb: number;\n duration: number;\n label: string;\n audioDuration: number;\n cancelled: boolean;\n charactersCount: number;\n streamed: boolean;\n error?: Error;\n}\n\nexport interface VADMetrics {\n timestamp: number;\n idleTime: number;\n inferenceDurationTotal: number;\n inferenceCount: number;\n label: string;\n}\n\nexport interface PipelineEOUMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n /** Timestamp of when the event was recorded */\n timestamp: number;\n /** Amount of time between the end of speech from VAD and the decision to end the user's turn */\n endOfUtteranceDelay: number;\n /**\n * Time taken to obtain the transcript after the end of the user's speech.\n *\n * @remarks\n * May be 0 if the transcript was already available.\n */\n transcriptionDelay: number;\n}\n\nexport interface PipelineLLMMetrics extends LLMMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n}\n\nexport interface PipelineTTSMetrics extends TTSMetrics {\n /**\n * Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics\n */\n sequenceId: string;\n}\n\nexport type PipelineSTTMetrics = STTMetrics;\nexport type PipelineVADMetrics = VADMetrics;\n\nexport class MultimodalLLMError extends Error {\n type?: string;\n reason?: string;\n code?: string;\n constructor(\n {\n type,\n reason,\n code,\n message,\n }: { type?: string; reason?: string; code?: string; message?: string } = {},\n options?: ErrorOptions,\n ) {\n super(message, options);\n this.type = type;\n this.reason = reason;\n this.code = code;\n }\n}\n\nexport interface MultimodalLLMMetrics extends LLMMetrics {\n inputTokenDetails: {\n cachedTokens: number;\n textTokens: number;\n audioTokens: number;\n };\n outputTokenDetails: {\n textTokens: number;\n audioTokens: number;\n };\n}\n\nexport type AgentMetrics =\n | STTMetrics\n | LLMMetrics\n | TTSMetrics\n | VADMetrics\n | PipelineSTTMetrics\n | PipelineEOUMetrics\n | PipelineLLMMetrics\n | PipelineTTSMetrics\n | PipelineVADMetrics\n | MultimodalLLMMetrics;\n"],"mappings":"AAoFO,MAAM,2BAA2B,MAAM;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA,YACE;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAyE,CAAC,GAC1E,SACA;AACA,UAAM,SAAS,OAAO;AACtB,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var metrics_exports = {};
20
+ __export(metrics_exports, {
21
+ MultimodalLLMError: () => import_base.MultimodalLLMError,
22
+ UsageCollector: () => import_usage_collector.UsageCollector,
23
+ logMetrics: () => import_utils.logMetrics
24
+ });
25
+ module.exports = __toCommonJS(metrics_exports);
26
+ var import_base = require("./base.cjs");
27
+ var import_usage_collector = require("./usage_collector.cjs");
28
+ var import_utils = require("./utils.cjs");
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ MultimodalLLMError,
32
+ UsageCollector,
33
+ logMetrics
34
+ });
35
+ //# sourceMappingURL=index.cjs.map