@zimtsui/typechat 0.0.61

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 (216) hide show
  1. package/DEVELOPMENT.md +22 -0
  2. package/LICENSE +165 -0
  3. package/README.md +202 -0
  4. package/assets/verbatim/declarations.handlebars +11 -0
  5. package/assets/verbatim/instruction.md +49 -0
  6. package/build/adaptor.d.ts +45 -0
  7. package/build/adaptor.js +90 -0
  8. package/build/adaptor.js.map +1 -0
  9. package/build/api-types/anthropic/billing.d.ts +12 -0
  10. package/build/api-types/anthropic/billing.js +14 -0
  11. package/build/api-types/anthropic/billing.js.map +1 -0
  12. package/build/api-types/anthropic/tool-codec.d.ts +16 -0
  13. package/build/api-types/anthropic/tool-codec.js +58 -0
  14. package/build/api-types/anthropic/tool-codec.js.map +1 -0
  15. package/build/api-types/google/billing.d.ts +12 -0
  16. package/build/api-types/google/billing.js +23 -0
  17. package/build/api-types/google/billing.js.map +1 -0
  18. package/build/api-types/google/restful-request.d.ts +8 -0
  19. package/build/api-types/google/restful-request.js +1 -0
  20. package/build/api-types/google/restful-request.js.map +1 -0
  21. package/build/api-types/google/tool-codec.d.ts +16 -0
  22. package/build/api-types/google/tool-codec.js +80 -0
  23. package/build/api-types/google/tool-codec.js.map +1 -0
  24. package/build/api-types/openai-chatcompletions/billing.d.ts +12 -0
  25. package/build/api-types/openai-chatcompletions/billing.js +14 -0
  26. package/build/api-types/openai-chatcompletions/billing.js.map +1 -0
  27. package/build/api-types/openai-chatcompletions/tool-codec.d.ts +17 -0
  28. package/build/api-types/openai-chatcompletions/tool-codec.js +80 -0
  29. package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -0
  30. package/build/api-types/openai-responses/billing.d.ts +12 -0
  31. package/build/api-types/openai-responses/billing.js +14 -0
  32. package/build/api-types/openai-responses/billing.js.map +1 -0
  33. package/build/api-types/openai-responses/tool-codec.d.ts +16 -0
  34. package/build/api-types/openai-responses/tool-codec.js +76 -0
  35. package/build/api-types/openai-responses/tool-codec.js.map +1 -0
  36. package/build/assets.d.ts +7 -0
  37. package/build/assets.js +8 -0
  38. package/build/assets.js.map +1 -0
  39. package/build/compatible-engine/session.d.ts +67 -0
  40. package/build/compatible-engine/session.js +92 -0
  41. package/build/compatible-engine/session.js.map +1 -0
  42. package/build/compatible-engine/structuring.d.ts +36 -0
  43. package/build/compatible-engine/structuring.js +34 -0
  44. package/build/compatible-engine/structuring.js.map +1 -0
  45. package/build/compatible-engine/validation.d.ts +21 -0
  46. package/build/compatible-engine/validation.js +82 -0
  47. package/build/compatible-engine/validation.js.map +1 -0
  48. package/build/compatible-engine.d/agentloop.d.ts +12 -0
  49. package/build/compatible-engine.d/agentloop.js +52 -0
  50. package/build/compatible-engine.d/agentloop.js.map +1 -0
  51. package/build/compatible-engine.d/aliyun/transport.d.ts +12 -0
  52. package/build/compatible-engine.d/aliyun/transport.js +7 -0
  53. package/build/compatible-engine.d/aliyun/transport.js.map +1 -0
  54. package/build/compatible-engine.d/aliyun.d.ts +24 -0
  55. package/build/compatible-engine.d/aliyun.js +46 -0
  56. package/build/compatible-engine.d/aliyun.js.map +1 -0
  57. package/build/compatible-engine.d/anthropic/choice-codec.d.ts +5 -0
  58. package/build/compatible-engine.d/anthropic/choice-codec.js +26 -0
  59. package/build/compatible-engine.d/anthropic/choice-codec.js.map +1 -0
  60. package/build/compatible-engine.d/anthropic/message-codec.d.ts +36 -0
  61. package/build/compatible-engine.d/anthropic/message-codec.js +86 -0
  62. package/build/compatible-engine.d/anthropic/message-codec.js.map +1 -0
  63. package/build/compatible-engine.d/anthropic/transport.d.ts +34 -0
  64. package/build/compatible-engine.d/anthropic/transport.js +150 -0
  65. package/build/compatible-engine.d/anthropic/transport.js.map +1 -0
  66. package/build/compatible-engine.d/anthropic.d.ts +26 -0
  67. package/build/compatible-engine.d/anthropic.js +46 -0
  68. package/build/compatible-engine.d/anthropic.js.map +1 -0
  69. package/build/compatible-engine.d/google/choice-codec.d.ts +5 -0
  70. package/build/compatible-engine.d/google/choice-codec.js +27 -0
  71. package/build/compatible-engine.d/google/choice-codec.js.map +1 -0
  72. package/build/compatible-engine.d/google/message-codec.d.ts +36 -0
  73. package/build/compatible-engine.d/google/message-codec.js +94 -0
  74. package/build/compatible-engine.d/google/message-codec.js.map +1 -0
  75. package/build/compatible-engine.d/google/transport.d.ts +30 -0
  76. package/build/compatible-engine.d/google/transport.js +78 -0
  77. package/build/compatible-engine.d/google/transport.js.map +1 -0
  78. package/build/compatible-engine.d/google.d.ts +26 -0
  79. package/build/compatible-engine.d/google.js +50 -0
  80. package/build/compatible-engine.d/google.js.map +1 -0
  81. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.d.ts +5 -0
  82. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js +31 -0
  83. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js.map +1 -0
  84. package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +24 -0
  85. package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +71 -0
  86. package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +1 -0
  87. package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.d.ts +38 -0
  88. package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js +77 -0
  89. package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js.map +1 -0
  90. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.d.ts +37 -0
  91. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js +190 -0
  92. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js.map +1 -0
  93. package/build/compatible-engine.d/openai-chatcompletions/transport.d.ts +10 -0
  94. package/build/compatible-engine.d/openai-chatcompletions/transport.js +11 -0
  95. package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +1 -0
  96. package/build/compatible-engine.d/openai-chatcompletions.d.ts +17 -0
  97. package/build/compatible-engine.d/openai-chatcompletions.js +14 -0
  98. package/build/compatible-engine.d/openai-chatcompletions.js.map +1 -0
  99. package/build/compatible-engine.d/openai-responses/choice-codec.d.ts +5 -0
  100. package/build/compatible-engine.d/openai-responses/choice-codec.js +30 -0
  101. package/build/compatible-engine.d/openai-responses/choice-codec.js.map +1 -0
  102. package/build/compatible-engine.d/openai-responses/message-codec.d.ts +36 -0
  103. package/build/compatible-engine.d/openai-responses/message-codec.js +96 -0
  104. package/build/compatible-engine.d/openai-responses/message-codec.js.map +1 -0
  105. package/build/compatible-engine.d/openai-responses/transport.d.ts +35 -0
  106. package/build/compatible-engine.d/openai-responses/transport.js +80 -0
  107. package/build/compatible-engine.d/openai-responses/transport.js.map +1 -0
  108. package/build/compatible-engine.d/openai-responses.d.ts +26 -0
  109. package/build/compatible-engine.d/openai-responses.js +46 -0
  110. package/build/compatible-engine.d/openai-responses.js.map +1 -0
  111. package/build/compatible-engine.d.ts +22 -0
  112. package/build/compatible-engine.js +29 -0
  113. package/build/compatible-engine.js.map +1 -0
  114. package/build/config.d.ts +24 -0
  115. package/build/config.js +11 -0
  116. package/build/config.js.map +1 -0
  117. package/build/endpoint-spec.d.ts +20 -0
  118. package/build/endpoint-spec.js +26 -0
  119. package/build/endpoint-spec.js.map +1 -0
  120. package/build/engine/session.d.ts +7 -0
  121. package/build/engine/session.js +1 -0
  122. package/build/engine/session.js.map +1 -0
  123. package/build/engine/transport.d.ts +5 -0
  124. package/build/engine/transport.js +1 -0
  125. package/build/engine/transport.js.map +1 -0
  126. package/build/engine/validation.d.ts +9 -0
  127. package/build/engine/validation.js +1 -0
  128. package/build/engine/validation.js.map +1 -0
  129. package/build/engine.d.ts +82 -0
  130. package/build/engine.js +104 -0
  131. package/build/engine.js.map +1 -0
  132. package/build/exports.d.ts +14 -0
  133. package/build/exports.js +15 -0
  134. package/build/exports.js.map +1 -0
  135. package/build/function.d.ts +106 -0
  136. package/build/function.js +55 -0
  137. package/build/function.js.map +1 -0
  138. package/build/inference-context.d.ts +6 -0
  139. package/build/inference-context.js +1 -0
  140. package/build/inference-context.js.map +1 -0
  141. package/build/native-engines.d/google/message-codec.d.ts +23 -0
  142. package/build/native-engines.d/google/message-codec.js +67 -0
  143. package/build/native-engines.d/google/message-codec.js.map +1 -0
  144. package/build/native-engines.d/google/session.d.ts +58 -0
  145. package/build/native-engines.d/google/session.js +101 -0
  146. package/build/native-engines.d/google/session.js.map +1 -0
  147. package/build/native-engines.d/google/transport.d.ts +34 -0
  148. package/build/native-engines.d/google/transport.js +81 -0
  149. package/build/native-engines.d/google/transport.js.map +1 -0
  150. package/build/native-engines.d/google/validation.d.ts +22 -0
  151. package/build/native-engines.d/google/validation.js +20 -0
  152. package/build/native-engines.d/google/validation.js.map +1 -0
  153. package/build/native-engines.d/google.d/agentloop.d.ts +12 -0
  154. package/build/native-engines.d/google.d/agentloop.js +57 -0
  155. package/build/native-engines.d/google.d/agentloop.js.map +1 -0
  156. package/build/native-engines.d/google.d/exports.d.ts +2 -0
  157. package/build/native-engines.d/google.d/exports.js +3 -0
  158. package/build/native-engines.d/google.d/exports.js.map +1 -0
  159. package/build/native-engines.d/google.d.ts +42 -0
  160. package/build/native-engines.d/google.js +83 -0
  161. package/build/native-engines.d/google.js.map +1 -0
  162. package/build/native-engines.d/openai-responses/choice-codec.d.ts +5 -0
  163. package/build/native-engines.d/openai-responses/choice-codec.js +30 -0
  164. package/build/native-engines.d/openai-responses/choice-codec.js.map +1 -0
  165. package/build/native-engines.d/openai-responses/message-codec.d.ts +23 -0
  166. package/build/native-engines.d/openai-responses/message-codec.js +75 -0
  167. package/build/native-engines.d/openai-responses/message-codec.js.map +1 -0
  168. package/build/native-engines.d/openai-responses/session.d.ts +61 -0
  169. package/build/native-engines.d/openai-responses/session.js +86 -0
  170. package/build/native-engines.d/openai-responses/session.js.map +1 -0
  171. package/build/native-engines.d/openai-responses/structuring.d.ts +19 -0
  172. package/build/native-engines.d/openai-responses/structuring.js +19 -0
  173. package/build/native-engines.d/openai-responses/structuring.js.map +1 -0
  174. package/build/native-engines.d/openai-responses/tool.d.ts +55 -0
  175. package/build/native-engines.d/openai-responses/tool.js +24 -0
  176. package/build/native-engines.d/openai-responses/tool.js.map +1 -0
  177. package/build/native-engines.d/openai-responses/transport.d.ts +35 -0
  178. package/build/native-engines.d/openai-responses/transport.js +84 -0
  179. package/build/native-engines.d/openai-responses/transport.js.map +1 -0
  180. package/build/native-engines.d/openai-responses/validation.d.ts +20 -0
  181. package/build/native-engines.d/openai-responses/validation.js +80 -0
  182. package/build/native-engines.d/openai-responses/validation.js.map +1 -0
  183. package/build/native-engines.d/openai-responses.d/agentloop.d.ts +12 -0
  184. package/build/native-engines.d/openai-responses.d/agentloop.js +61 -0
  185. package/build/native-engines.d/openai-responses.d/agentloop.js.map +1 -0
  186. package/build/native-engines.d/openai-responses.d/exports.d.ts +2 -0
  187. package/build/native-engines.d/openai-responses.d/exports.js +3 -0
  188. package/build/native-engines.d/openai-responses.d/exports.js.map +1 -0
  189. package/build/native-engines.d/openai-responses.d.ts +44 -0
  190. package/build/native-engines.d/openai-responses.js +78 -0
  191. package/build/native-engines.d/openai-responses.js.map +1 -0
  192. package/build/telemetry.d.ts +6 -0
  193. package/build/telemetry.js +19 -0
  194. package/build/telemetry.js.map +1 -0
  195. package/build/throttle.d.ts +11 -0
  196. package/build/throttle.js +39 -0
  197. package/build/throttle.js.map +1 -0
  198. package/build/tsconfig.tsbuildinfo +1 -0
  199. package/build/verbatim/codec/declarations.d.ts +2 -0
  200. package/build/verbatim/codec/declarations.js +22 -0
  201. package/build/verbatim/codec/declarations.js.map +1 -0
  202. package/build/verbatim/codec/request.d.ts +7 -0
  203. package/build/verbatim/codec/request.js +53 -0
  204. package/build/verbatim/codec/request.js.map +1 -0
  205. package/build/verbatim/codec.d.ts +2 -0
  206. package/build/verbatim/codec.js +3 -0
  207. package/build/verbatim/codec.js.map +1 -0
  208. package/build/verbatim.d.ts +74 -0
  209. package/build/verbatim.js +17 -0
  210. package/build/verbatim.js.map +1 -0
  211. package/examples/agent.ts +65 -0
  212. package/examples/chat.ts +24 -0
  213. package/examples/config.ts +24 -0
  214. package/examples/tsconfig.json +28 -0
  215. package/examples/verbatim.ts +44 -0
  216. package/package.json +50 -0
package/DEVELOPMENT.md ADDED
@@ -0,0 +1,22 @@
1
+ ## Architecture Diagram
2
+
3
+ ```mermaid
4
+ classDiagram
5
+
6
+ Engine <|.. CompatibleEngine
7
+
8
+ Engine <|.. OpenAIResponsesNativeEngine
9
+ OpenAIResponsesNativeEngine o--> OpenAIResponsesNativeHelpers
10
+ OpenAIResponsesNativeHelpers o--> OpenAIResponsesHelpers
11
+
12
+ CompatibleEngine <|.. AliyunEngine
13
+ OpenAIChatCompletionsCompatibleHelpers o--> OpenAIChatCompletionsHelpers
14
+ OpenAIChatCompletionsCompatibleMonolithHelpers o--> OpenAIChatCompletionsCompatibleHelpers
15
+ OpenAIChatCompletionsCompatibleStreamHelpers o--> OpenAIChatCompletionsCompatibleHelpers
16
+ AliyunEngine o--> OpenAIChatCompletionsCompatibleStreamHelpers
17
+
18
+ CompatibleEngine <|.. OpenAICompatibleEngine
19
+ OpenAICompatibleEngine o--> OpenAIResponsesCompatibleHelpers
20
+ OpenAIResponsesCompatibleHelpers o--> OpenAIResponsesHelpers
21
+
22
+ ```
package/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # TypeChat
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/@zimtsui/typechat?style=flat-square)](https://www.npmjs.com/package/@zimtsui/typechat)
4
+
5
+ TypeChat 是一个强类型的 LLM 推理 API 适配器。
6
+
7
+ ## 支持服务商 API 类型
8
+
9
+ - OpenAI Responses
10
+ - Google
11
+ - 阿里云 OpenAI Chat Completions Compatible
12
+ - Anthropic
13
+
14
+ ## 核心概念
15
+
16
+ - `Session`:会话状态。
17
+ - `InferenceContext`:工作流环境,包含 [Typelemetry](https://github.com/zimtsui/typelemetry) Logger、`AbortSignal`、用户防止并发过载的[读写锁](https://github.com/zimtsui/typelocks)。
18
+ - `Engine`:推理引擎,从一个会话状态生成下一个会话状态。
19
+ - `Endpoint`:代表一家服务商的一个模型的 API 端点。
20
+ - `Adaptor`:Engine 工厂。
21
+ - `RoleMessage`:三类角色消息 `Developer`、`User`、`AI`,消息由 `Text/Function.Call/Response` 片段组成。
22
+ - `Function.Declaration.Map`:函数工具声明集合,使用 [JSON Schema](https://json-schema.org/) 描述函数参数。
23
+
24
+ ## 配置
25
+
26
+ ```ts
27
+ import { type Config } from '@zimtsui/typechat';
28
+
29
+ // 配置推理服务商 API 接入点
30
+ export const config: Config = {
31
+ typechat: {
32
+ endpoints: {
33
+ 'gpt-5.4-mini': {
34
+ name: 'GPT-5.4 mini',
35
+ apiType: 'openai-responses',
36
+ baseUrl: 'https://api.openai.com/v1',
37
+ apiKey: process.env.OPENAI_API_KEY!,
38
+ model: 'gpt-5-mini',
39
+ },
40
+ 'gemini-3-flash': {
41
+ name: 'Gemini 3 Flash',
42
+ apiType: 'google',
43
+ baseUrl: 'https://generativelanguage.googleapis.com',
44
+ apiKey: process.env.GOOGLE_API_KEY!,
45
+ model: 'gemini-3-flash',
46
+ },
47
+ }
48
+ }
49
+ }
50
+ ```
51
+
52
+ ## 对话
53
+
54
+ ```ts
55
+ import { Adaptor, RoleMessage, type Session } from '@zimtsui/typechat';
56
+ import { config } from './config.ts';
57
+
58
+
59
+ // 创建会话
60
+ const session: Session<never, never> = {
61
+ developerMessage: new RoleMessage.Developer([
62
+ RoleMessage.Part.Text.paragraph('You are a helpful assistant.'),
63
+ ]),
64
+ chatMessages: [
65
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('Hello!') ]),
66
+ ],
67
+ };
68
+
69
+ // 选择推理引擎
70
+ const adaptor = Adaptor.create(config);
71
+ const engine = adaptor.makeCompatibleEngine<{}, {}>({
72
+ endpoint: 'gpt-5.4-mini',
73
+ functionDeclarationMap: {},
74
+ verbatimDeclarationMap: {},
75
+ });
76
+
77
+ const response = await engine.stateless({}, session);
78
+ console.log(response.getText());
79
+ ```
80
+
81
+ ## 智能体
82
+
83
+ ```ts
84
+ import { Adaptor, agentloop, RoleMessage, Function, type Session, Structuring } from '@zimtsui/typechat';
85
+ import { Type } from '@sinclair/typebox';
86
+ import { config } from './config.ts';
87
+
88
+ // 声明函数工具
89
+ const fdm = {
90
+ get_weather: {
91
+ description: '获取指定城市的天气',
92
+ parameters: Type.Object({
93
+ city: Type.String(),
94
+ unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
95
+ }),
96
+ },
97
+ submit_result: {
98
+ description: '提交最终结果',
99
+ parameters: Type.Object({
100
+ weather: Type.String(),
101
+ advice: Type.String(),
102
+ }),
103
+ },
104
+ } satisfies Function.Decl.Map.Proto;
105
+ type fdm = typeof fdm;
106
+ type fdu = Function.Decl.From<fdm>;
107
+
108
+ // 实现函数工具
109
+ export class Submission {
110
+ public constructor(public weather: string, public advice: string) {}
111
+ }
112
+ const fnm: Function.Map<fdm> = {
113
+ async get_weather({ city, unit }) {
114
+ const data = { city, unit: unit ?? 'C', temperature: 26, sky: 'sunny' };
115
+ return JSON.stringify(data);
116
+ },
117
+ async submit_result({ weather, advice }) {
118
+ throw new Submission(weather, advice);
119
+ },
120
+ };
121
+
122
+ // 创建会话
123
+ const session: Session<fdu, never> = {
124
+ developerMessage: new RoleMessage.Developer([
125
+ RoleMessage.Part.Text.paragraph('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
126
+ ]),
127
+ chatMessages: [
128
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请查询现在北京的天气,并给穿衣建议。') ]),
129
+ ],
130
+ };
131
+
132
+ // 选择推理引擎
133
+ const adaptor = Adaptor.create(config);
134
+ const engine = adaptor.makeCompatibleEngine<fdm, {}>({
135
+ endpoint: 'gpt-5.4-mini',
136
+ functionDeclarationMap: fdm,
137
+ verbatimDeclarationMap: {},
138
+ structuringChoice: Structuring.Choice.FCall.REQUIRED,
139
+ });
140
+
141
+ // 使用 agentloop 驱动智能体循环,最多 8 轮对话
142
+ try {
143
+ for await (const text of agentloop({}, session, engine, fnm, 8)) console.log(text);
144
+ } catch (e) {
145
+ if (e instanceof Submission) {} else throw e;
146
+ console.log(e.weather);
147
+ console.log(e.advice);
148
+ }
149
+ ```
150
+
151
+ ### XML Verbatim 频道
152
+
153
+ When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
154
+
155
+ XML Verbatim Channel is designed to avoid escaping in structured output of large text.
156
+
157
+ ```ts
158
+ import { Adaptor, RoleMessage, type Session, Structuring, Verbatim } from '@zimtsui/typechat';
159
+ import Assets from '@zimtsui/typechat/assets';
160
+ import * as Codec from '@zimtsui/typechat/codec';
161
+ import { config } from './config.ts';
162
+
163
+ // 声明 XML Verbatim 频道
164
+ const vdm = {
165
+ bash: {
166
+ description: '执行 Bash 命令',
167
+ parameters: {
168
+ command: {
169
+ description: 'Bash 命令',
170
+ mimeType: 'text/plain',
171
+ },
172
+ },
173
+ },
174
+ } satisfies Verbatim.Decl.Map.Proto;
175
+ type vdm = typeof vdm;
176
+ type vdu = Verbatim.Decl.From<vdm>;
177
+
178
+
179
+ // 创建会话
180
+ const session: Session<never, vdu> = {
181
+ developerMessage: new RoleMessage.Developer([
182
+ RoleMessage.Part.Text.paragraph(Assets.verbatim.instruction),
183
+ RoleMessage.Part.Text.paragraph('# Available Verbatim Channels'),
184
+ RoleMessage.Part.Text.paragraph(Codec.Declarations.encode(vdm)),
185
+ ]),
186
+ chatMessages: [
187
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请使用 Bash 命令查询当前系统时间。') ]),
188
+ ],
189
+ };
190
+
191
+ // 选择推理引擎
192
+ const adaptor = Adaptor.create(config);
193
+ const engine = adaptor.makeCompatibleEngine<{}, vdm>({
194
+ endpoint: 'gpt-5.4-mini',
195
+ functionDeclarationMap: {},
196
+ verbatimDeclarationMap: vdm,
197
+ structuringChoice: Structuring.Choice.VRequest.ANYONE,
198
+ });
199
+
200
+ const response = await engine.stateless({}, session);
201
+ console.log(response.getOnlyVerbatimRequest().args.command);
202
+ ```
@@ -0,0 +1,11 @@
1
+ {{#each declarations}}
2
+ <verbatim:declaration name="{{this.name}}">
3
+ <verbatim:description>{{this.description}}</verbatim:description>
4
+ {{#each this.parameters}}
5
+ <verbatim:parameter name="{{this.name}}">
6
+ <verbatim:description>{{this.description}}</verbatim:description>
7
+ <verbatim:mime-type>{{this.mimeType}}</verbatim:mime-type>
8
+ </verbatim:parameter>
9
+ {{/each}}
10
+ </verbatim:declaration>
11
+ {{/each}}
@@ -0,0 +1,49 @@
1
+ # XML Verbatim Channel
2
+
3
+ ## Motivation
4
+
5
+ When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
6
+
7
+ XML Verbatim Channel is designed to avoid escaping in structured output of large text.
8
+
9
+ ## Declaration
10
+
11
+ The system or the user will declare all available XML Verbatim Channels in the form of
12
+
13
+ <verbatim:declaration name="CHANNEL_1_NAME">
14
+ <verbatim:description>DESCRIPTION_OF_CHANNEL_1</verbatim:description>
15
+ <verbatim:parameter name="PARAMETER_1_NAME">
16
+ <verbatim:description>DESCRIPTION_OF_PARAMETER_1</verbatim:description>
17
+ <verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_1</verbatim:mime-type>
18
+ </verbatim:parameter>
19
+ <verbatim:parameter name="PARAMETER_2_NAME">
20
+ <verbatim:description>DESCRIPTION_OF_PARAMETER_2</verbatim:description>
21
+ <verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_2</verbatim:mime-type>
22
+ </verbatim:parameter>
23
+ </verbatim:declaration>
24
+
25
+ ## Request
26
+
27
+ You can make a request through the channel CHANNEL_1_NAME in the form of
28
+
29
+ <verbatim:request name="CHANNEL_1_NAME">
30
+ <verbatim:argument name="PARAMETER_1_NAME"><![CDATA[PARAMETER_1_VALUE]]></verbatim:argument>
31
+ <verbatim:argument name="PARAMETER_2_NAME"><![CDATA[PARAMETER_2_VALUE]]></verbatim:argument>
32
+ </verbatim:request>
33
+
34
+ - All parameters are required
35
+ - All arguments must be wrapped in CDATA
36
+
37
+ # Available XML Verbatim Channels
38
+
39
+ <verbatim:declaration name="submit">
40
+ <verbatim:description>提交文章</verbatim:description>
41
+ <verbatim:parameter name="title">
42
+ <verbatim:description>文章标题</verbatim:description>
43
+ <verbatim:mime-type>text/plain</verbatim:mime-type>
44
+ </verbatim:parameter>
45
+ <verbatim:parameter name="body">
46
+ <verbatim:description>文章内容</verbatim:description>
47
+ <verbatim:mime-type>text/markdown;dialect=gfm+tex_math_dollars</verbatim:mime-type>
48
+ </verbatim:parameter>
49
+ </verbatim:declaration>
@@ -0,0 +1,45 @@
1
+ import { Config } from './config.ts';
2
+ import { Function } from './function.ts';
3
+ import { type CompatibleEngine } from './compatible-engine.ts';
4
+ import { Throttle } from './throttle.ts';
5
+ import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses.ts';
6
+ import { GoogleNativeEngine } from './native-engines.d/google.ts';
7
+ import type { Verbatim } from './verbatim.ts';
8
+ import type { Structuring } from './compatible-engine/structuring.ts';
9
+ import type { Structuring as OpenAIResponsesNativeStructuring } from './native-engines.d/openai-responses/structuring.ts';
10
+ export declare class Adaptor {
11
+ config: Config;
12
+ static create(config: Config): Adaptor;
13
+ protected throttles: Map<string, Throttle>;
14
+ protected constructor(config: Config);
15
+ makeCompatibleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.CompatibleEngine.Options<fdm, vdm>): CompatibleEngine<fdm, vdm>;
16
+ makeOpenAIResponsesNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.OpenAIResponsesNativeEngineOptions<fdm, vdm>): OpenAIResponsesNativeEngine<fdm, vdm>;
17
+ makeGoogleNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.GoogleNativeEngineOptions<fdm, vdm>): GoogleNativeEngine<fdm, vdm>;
18
+ }
19
+ export declare namespace Adaptor {
20
+ namespace CompatibleEngine {
21
+ interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
22
+ endpoint: string;
23
+ functionDeclarationMap: fdm;
24
+ verbatimDeclarationMap: vdm;
25
+ structuringChoice?: Structuring.Choice.From<fdm, vdm>;
26
+ }
27
+ }
28
+ interface OpenAIResponsesNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
29
+ endpoint: string;
30
+ functionDeclarationMap: fdm;
31
+ verbatimDeclarationMap: vdm;
32
+ applyPatch?: boolean;
33
+ structuringChoice?: OpenAIResponsesNativeStructuring.Choice.From<fdm, vdm>;
34
+ }
35
+ interface GoogleNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
36
+ endpoint: string;
37
+ functionDeclarationMap: fdm;
38
+ verbatimDeclarationMap: vdm;
39
+ structuringChoice?: Structuring.Choice.From<fdm, vdm>;
40
+ codeExecution?: boolean;
41
+ urlContext?: boolean;
42
+ googleSearch?: boolean;
43
+ parallelToolCall?: boolean;
44
+ }
45
+ }
@@ -0,0 +1,90 @@
1
+ import { Throttle } from "./throttle.js";
2
+ import { GoogleCompatibleEngine } from "./compatible-engine.d/google.js";
3
+ import { OpenAIResponsesCompatibleEngine } from "./compatible-engine.d/openai-responses.js";
4
+ import { AnthropicCompatibleEngine } from "./compatible-engine.d/anthropic.js";
5
+ import { AliyunCompatibleEngine } from "./compatible-engine.d/aliyun.js";
6
+ import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses.js";
7
+ import { GoogleNativeEngine } from "./native-engines.d/google.js";
8
+ export class Adaptor {
9
+ config;
10
+ static create(config) {
11
+ return new Adaptor(config);
12
+ }
13
+ throttles = new Map();
14
+ constructor(config) {
15
+ this.config = config;
16
+ for (const endpointId in this.config.typechat.endpoints) {
17
+ const rpm = this.config.typechat.endpoints[endpointId].rpm ?? Number.POSITIVE_INFINITY;
18
+ this.throttles.set(endpointId, new Throttle(rpm));
19
+ }
20
+ }
21
+ makeCompatibleEngine(adaptorOptions) {
22
+ const endpointSpec = this.config.typechat.endpoints[adaptorOptions.endpoint];
23
+ if (endpointSpec) { }
24
+ else
25
+ throw new Error();
26
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
27
+ if (throttle) { }
28
+ else
29
+ throw new Error();
30
+ const options = {
31
+ ...endpointSpec,
32
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
33
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
34
+ structuringChoice: adaptorOptions.structuringChoice,
35
+ throttle,
36
+ };
37
+ if (endpointSpec.apiType === 'openai-responses')
38
+ return new OpenAIResponsesCompatibleEngine.Instance(options);
39
+ else if (endpointSpec.apiType === 'google')
40
+ return new GoogleCompatibleEngine.Instance(options);
41
+ else if (endpointSpec.apiType === 'aliyun')
42
+ return new AliyunCompatibleEngine.Instance(options);
43
+ else if (endpointSpec.apiType === 'anthropic')
44
+ return new AnthropicCompatibleEngine.Instance(options);
45
+ else
46
+ throw new Error();
47
+ }
48
+ makeOpenAIResponsesNativeEngine(adaptorOptions) {
49
+ const endpointSpec = this.config.typechat.endpoints[adaptorOptions.endpoint];
50
+ if (endpointSpec?.apiType === 'openai-responses') { }
51
+ else
52
+ throw new Error();
53
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
54
+ if (throttle) { }
55
+ else
56
+ throw new Error();
57
+ const options = {
58
+ ...endpointSpec,
59
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
60
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
61
+ structuringChoice: adaptorOptions.structuringChoice,
62
+ throttle,
63
+ applyPatch: adaptorOptions.applyPatch,
64
+ };
65
+ return new OpenAIResponsesNativeEngine.Instance(options);
66
+ }
67
+ makeGoogleNativeEngine(adaptorOptions) {
68
+ const endpointSpec = this.config.typechat.endpoints[adaptorOptions.endpoint];
69
+ if (endpointSpec?.apiType === 'google') { }
70
+ else
71
+ throw new Error();
72
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
73
+ if (throttle) { }
74
+ else
75
+ throw new Error();
76
+ const options = {
77
+ ...endpointSpec,
78
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
79
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
80
+ structuringChoice: adaptorOptions.structuringChoice,
81
+ parallelToolCall: adaptorOptions.parallelToolCall,
82
+ throttle,
83
+ codeExecution: adaptorOptions.codeExecution,
84
+ urlContext: adaptorOptions.urlContext,
85
+ googleSearch: adaptorOptions.googleSearch,
86
+ };
87
+ return new GoogleNativeEngine.Instance(options);
88
+ }
89
+ }
90
+ //# sourceMappingURL=adaptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,12 @@
1
+ import type { Pricing } from '../../engine.ts';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
+ export declare class Billing {
4
+ protected ctx: Billing.Context;
5
+ constructor(ctx: Billing.Context);
6
+ charge(usage: Anthropic.Usage): number;
7
+ }
8
+ export declare namespace Billing {
9
+ interface Context {
10
+ pricing: Pricing;
11
+ }
12
+ }
@@ -0,0 +1,14 @@
1
+ export class Billing {
2
+ ctx;
3
+ constructor(ctx) {
4
+ this.ctx = ctx;
5
+ }
6
+ charge(usage) {
7
+ const cacheHitTokenCount = usage.cache_read_input_tokens || 0;
8
+ const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
9
+ return (this.ctx.pricing.inputPrice * cacheMissTokenCount / 1e6 +
10
+ this.ctx.pricing.cachePrice * cacheHitTokenCount / 1e6 +
11
+ this.ctx.pricing.outputPrice * usage.output_tokens / 1e6);
12
+ }
13
+ }
14
+ //# sourceMappingURL=billing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;IACa;IAA7B,YAA6B,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE9C,MAAM,CAAC,KAAsB;QAChC,MAAM,kBAAkB,GAAG,KAAK,CAAC,uBAAuB,IAAI,CAAC,CAAC;QAC9D,MAAM,mBAAmB,GAAG,KAAK,CAAC,YAAY,GAAG,kBAAkB,CAAC;QACpE,OAAO,CACH,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACvD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YACtD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CAC3D,CAAC;IACN,CAAC;CACJ"}
@@ -0,0 +1,16 @@
1
+ import { Function } from '../../function.ts';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
+ export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
4
+ protected ctx: ToolCodec.Context<fdm>;
5
+ constructor(ctx: ToolCodec.Context<fdm>);
6
+ encodeFunctionCall(fc: Function.Call.From<fdm>): Anthropic.ToolUseBlock;
7
+ decodeFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.From<fdm>;
8
+ encodeFunctionResponse(fr: Function.Response.From<fdm>): Anthropic.ToolResultBlockParam;
9
+ protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): Anthropic.Tool;
10
+ encodeFunctionDeclarationMap(fdm: fdm): Anthropic.Tool[];
11
+ }
12
+ export declare namespace ToolCodec {
13
+ interface Context<in out fdm extends Function.Decl.Map.Proto> {
14
+ fdm: fdm;
15
+ }
16
+ }