@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
@@ -0,0 +1,58 @@
1
+ import { Function } from "../../function.js";
2
+ import { ResponseInvalid } from "../../engine.js";
3
+ import Ajv from 'ajv';
4
+ const ajv = new Ajv();
5
+ export class ToolCodec {
6
+ ctx;
7
+ constructor(ctx) {
8
+ this.ctx = ctx;
9
+ }
10
+ encodeFunctionCall(fc) {
11
+ throw new Error('Anthropic compatible engine requires native function calls.');
12
+ }
13
+ decodeFunctionCall(apifc) {
14
+ const fditem = this.ctx.fdm[apifc.name];
15
+ if (fditem) { }
16
+ else
17
+ throw new ResponseInvalid('Unknown function call', { cause: apifc });
18
+ if (ajv.validate(fditem.parameters, apifc.input)) { }
19
+ else
20
+ throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
21
+ return Function.Call.of({
22
+ id: apifc.id,
23
+ name: apifc.name,
24
+ args: apifc.input,
25
+ });
26
+ }
27
+ encodeFunctionResponse(fr) {
28
+ if (fr.id) { }
29
+ else
30
+ throw new Error();
31
+ if (fr instanceof Function.Response.Successful)
32
+ return {
33
+ type: 'tool_result',
34
+ tool_use_id: fr.id,
35
+ content: fr.text,
36
+ };
37
+ else if (fr instanceof Function.Response.Failed)
38
+ return {
39
+ type: 'tool_result',
40
+ tool_use_id: fr.id,
41
+ content: fr.error,
42
+ };
43
+ else
44
+ throw new Error();
45
+ }
46
+ encodeFunctionDeclarationEntry(fdentry) {
47
+ return {
48
+ name: fdentry[0],
49
+ description: fdentry[1].description,
50
+ input_schema: fdentry[1].parameters,
51
+ };
52
+ }
53
+ encodeFunctionDeclarationMap(fdm) {
54
+ const fdentries = Object.entries(fdm);
55
+ return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
56
+ }
57
+ }
58
+ //# sourceMappingURL=tool-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-codec.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AAQ,CAAC,UAAU;YAC1C,OAAO;gBACH,IAAI,EAAE,aAAa;gBACnB,WAAW,EAAE,EAAE,CAAC,EAAE;gBAClB,OAAO,EAAE,EAAE,CAAC,IAAI;aACnB,CAAC;aACD,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,MAAM;YAC3C,OAAO;gBACH,IAAI,EAAE,aAAa;gBACnB,WAAW,EAAE,EAAE,CAAC,EAAE;gBAClB,OAAO,EAAE,EAAE,CAAC,KAAK;aACpB,CAAC;;YACD,MAAM,IAAI,KAAK,EAAE,CAAC;IAC3B,CAAC;IAES,8BAA8B,CACpC,OAAsC;QAEtC,OAAO;YACH,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;YAChB,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW;YACnC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,UAAqB;SACjD,CAAC;IACN,CAAC;IAEM,4BAA4B,CAC/B,GAAQ;QAER,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAoC,CAAC;QAEzE,OAAO,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC,CAAC;IAClF,CAAC;CAEJ"}
@@ -0,0 +1,12 @@
1
+ import * as Google from '@google/genai';
2
+ import type { Pricing } from '../../engine.ts';
3
+ export declare class Billing {
4
+ protected ctx: Billing.Context;
5
+ constructor(ctx: Billing.Context);
6
+ charge(usageMetadata: Google.GenerateContentResponseUsageMetadata): number;
7
+ }
8
+ export declare namespace Billing {
9
+ interface Context {
10
+ pricing: Pricing;
11
+ }
12
+ }
@@ -0,0 +1,23 @@
1
+ import { logger } from "../../telemetry.js";
2
+ export class Billing {
3
+ ctx;
4
+ constructor(ctx) {
5
+ this.ctx = ctx;
6
+ }
7
+ charge(usageMetadata) {
8
+ logger.message.debug(usageMetadata);
9
+ if (usageMetadata.promptTokenCount) { }
10
+ else
11
+ throw new Error('Prompt token count missing.', { cause: usageMetadata });
12
+ const candidatesTokenCount = usageMetadata.candidatesTokenCount ?? 0;
13
+ const cacheHitTokenCount = usageMetadata.cachedContentTokenCount ?? 0;
14
+ const cacheMissTokenCount = usageMetadata.promptTokenCount - cacheHitTokenCount;
15
+ const thinkingTokenCount = usageMetadata.thoughtsTokenCount ?? 0;
16
+ const cost = this.ctx.pricing.inputPrice * cacheMissTokenCount / 1e6 +
17
+ this.ctx.pricing.cachePrice * cacheHitTokenCount / 1e6 +
18
+ this.ctx.pricing.outputPrice * candidatesTokenCount / 1e6 +
19
+ this.ctx.pricing.outputPrice * thinkingTokenCount / 1e6;
20
+ return cost;
21
+ }
22
+ }
23
+ //# sourceMappingURL=billing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/google/billing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,MAAM,OAAO,OAAO;IACa;IAA7B,YAA6B,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE9C,MAAM,CAAC,aAA0D;QACpE,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;QAEpC,IAAI,aAAa,CAAC,gBAAgB,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,CAAC,6BAA6B,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;QACrH,MAAM,oBAAoB,GAAG,aAAa,CAAC,oBAAoB,IAAI,CAAC,CAAC;QACrE,MAAM,kBAAkB,GAAG,aAAa,CAAC,uBAAuB,IAAI,CAAC,CAAC;QACtE,MAAM,mBAAmB,GAAG,aAAa,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;QAChF,MAAM,kBAAkB,GAAG,aAAa,CAAC,kBAAkB,IAAI,CAAC,CAAC;QACjE,MAAM,IAAI,GACN,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,oBAAoB,GAAG,GAAG;YACzD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,kBAAkB,GAAG,GAAG,CAAC;QAC5D,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ"}
@@ -0,0 +1,8 @@
1
+ import * as Google from '@google/genai';
2
+ export interface RestfulRequest {
3
+ contents: Google.Content[];
4
+ tools?: Google.Tool[];
5
+ toolConfig?: Google.ToolConfig;
6
+ systemInstruction?: Google.Content;
7
+ generationConfig?: Google.GenerationConfig;
8
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=restful-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restful-request.js","sourceRoot":"","sources":["../../../src/api-types/google/restful-request.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import { Function } from '../../function.ts';
2
+ import * as Google from '@google/genai';
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>): Google.FunctionCall;
7
+ encodeFunctionDeclarationMap(fdm: fdm): Google.FunctionDeclaration[];
8
+ protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): Google.FunctionDeclaration;
9
+ decodeFunctionCall(googlefc: Google.FunctionCall): Function.Call.From<fdm>;
10
+ encodeFunctionResponse(fr: Function.Response.From<fdm>): Google.Part;
11
+ }
12
+ export declare namespace ToolCodec {
13
+ interface Context<in out fdm extends Function.Decl.Map.Proto> {
14
+ fdm: fdm;
15
+ }
16
+ }
@@ -0,0 +1,80 @@
1
+ import { ResponseInvalid } from "../../engine.js";
2
+ import { Function } from "../../function.js";
3
+ import * as Google from '@google/genai';
4
+ import Ajv from 'ajv';
5
+ const ajv = new Ajv();
6
+ export class ToolCodec {
7
+ ctx;
8
+ constructor(ctx) {
9
+ this.ctx = ctx;
10
+ }
11
+ encodeFunctionCall(fc) {
12
+ return {
13
+ id: fc.id,
14
+ name: fc.name,
15
+ args: fc.args,
16
+ };
17
+ }
18
+ encodeFunctionDeclarationMap(fdm) {
19
+ const fdentries = Object.entries(fdm);
20
+ return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
21
+ }
22
+ encodeFunctionDeclarationEntry(fdentry) {
23
+ const json = JSON.stringify(fdentry[1].parameters);
24
+ const parsed = JSON.parse(json, (key, value) => {
25
+ if (key === 'type' && typeof value === 'string') {
26
+ if (value === 'string')
27
+ return Google.Type.STRING;
28
+ else if (value === 'number')
29
+ return Google.Type.NUMBER;
30
+ else if (value === 'boolean')
31
+ return Google.Type.BOOLEAN;
32
+ else if (value === 'object')
33
+ return Google.Type.OBJECT;
34
+ else if (value === 'array')
35
+ return Google.Type.ARRAY;
36
+ else
37
+ throw new Error();
38
+ }
39
+ else if (key === 'additionalProperties' && typeof value === 'boolean')
40
+ return;
41
+ else
42
+ return value;
43
+ });
44
+ return {
45
+ name: fdentry[0],
46
+ description: fdentry[1].description,
47
+ parameters: parsed,
48
+ };
49
+ }
50
+ decodeFunctionCall(googlefc) {
51
+ if (googlefc.name) { }
52
+ else
53
+ throw new Error();
54
+ const fditem = this.ctx.fdm[googlefc.name];
55
+ if (fditem) { }
56
+ else
57
+ throw new ResponseInvalid('Unknown function call', { cause: googlefc });
58
+ if (ajv.validate(fditem.parameters, googlefc.args)) { }
59
+ else
60
+ throw new ResponseInvalid('Function call not conforming to schema', { cause: googlefc });
61
+ return Function.Call.of({
62
+ id: googlefc.id,
63
+ name: googlefc.name,
64
+ args: googlefc.args,
65
+ });
66
+ }
67
+ encodeFunctionResponse(fr) {
68
+ if (fr instanceof Function.Response.Successful)
69
+ return {
70
+ functionResponse: { id: fr.id, name: fr.name, response: { output: fr.text } },
71
+ };
72
+ else if (fr instanceof Function.Response.Failed)
73
+ return {
74
+ functionResponse: { id: fr.id, name: fr.name, response: { error: fr.error } },
75
+ };
76
+ else
77
+ throw new Error();
78
+ }
79
+ }
80
+ //# sourceMappingURL=tool-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-codec.js","sourceRoot":"","sources":["../../../src/api-types/google/tool-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAItB,MAAM,OAAO,SAAS;IACW;IAA7B,YAA6B,GAA2B;QAA3B,QAAG,GAAH,GAAG,CAAwB;IAAG,CAAC;IAErD,kBAAkB,CACrB,EAA2B;QAE3B,OAAO;YACH,EAAE,EAAE,EAAE,CAAC,EAAE;YACT,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,IAAI,EAAE,EAAE,CAAC,IAA+B;SAC3C,CAAC;IACN,CAAC;IAEM,4BAA4B,CAAC,GAAQ;QACxC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAoC,CAAC;QACzE,OAAO,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC,CAAC;IAClF,CAAC;IAES,8BAA8B,CACpC,OAAsC;QAEtC,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QACnD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EA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Q,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA,CAAC;;YACjD,MAAM,IAAI,eAAe,CAAC,wCAAwC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC9F,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACpB,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,IAAI,EAAE,QAAQ,CAAC,IAAI;YACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;SACa,CAAC,CAAC;IAC1C,CAAC;IAEM,sBAAsB,CACzB,EAA+B;QAE/B,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,UAAU;YAAE,OAAO;gBACnD,gBAAgB,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE;aAChF,CAAC;aAAM,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,MAAM;YAAE,OAAO;gBACvD,gBAAgB,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE;aAChF,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;IAC9B,CAAC;CAEJ"}
@@ -0,0 +1,12 @@
1
+ import type { Pricing } from '../../engine.ts';
2
+ import OpenAI from 'openai';
3
+ export declare class OpenAIChatCompletionsBilling {
4
+ protected ctx: OpenAIChatCompletionsBilling.Context;
5
+ constructor(ctx: OpenAIChatCompletionsBilling.Context);
6
+ charge(usage: OpenAI.CompletionUsage): number;
7
+ }
8
+ export declare namespace OpenAIChatCompletionsBilling {
9
+ interface Context {
10
+ pricing: Pricing;
11
+ }
12
+ }
@@ -0,0 +1,14 @@
1
+ export class OpenAIChatCompletionsBilling {
2
+ ctx;
3
+ constructor(ctx) {
4
+ this.ctx = ctx;
5
+ }
6
+ charge(usage) {
7
+ const cacheHitTokenCount = usage.prompt_tokens_details?.cached_tokens ?? 0;
8
+ const cacheMissTokenCount = usage.prompt_tokens - cacheHitTokenCount;
9
+ return (this.ctx.pricing.inputPrice * cacheMissTokenCount / 1e6 +
10
+ this.ctx.pricing.cachePrice * cacheHitTokenCount / 1e6 +
11
+ this.ctx.pricing.outputPrice * usage.completion_tokens / 1e6);
12
+ }
13
+ }
14
+ //# sourceMappingURL=billing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/openai-chatcompletions/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,4BAA4B;IACR;IAA7B,YAA6B,GAAyC;QAAzC,QAAG,GAAH,GAAG,CAAsC;IAAG,CAAC;IAEnE,MAAM,CAAC,KAA6B;QACvC,MAAM,kBAAkB,GAAG,KAAK,CAAC,qBAAqB,EAAE,aAAa,IAAI,CAAC,CAAC;QAC3E,MAAM,mBAAmB,GAAG,KAAK,CAAC,aAAa,GAAG,kBAAkB,CAAC;QACrE,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,iBAAiB,GAAG,GAAG,CAC/D,CAAC;IACN,CAAC;CACJ"}
@@ -0,0 +1,17 @@
1
+ import { Function } from '../../function.ts';
2
+ import OpenAI from 'openai';
3
+ export declare class OpenAIChatCompletionsToolCodec<in out fdm extends Function.Decl.Map.Proto> {
4
+ protected ctx: OpenAIChatCompletionsToolCodec.Context<fdm>;
5
+ constructor(ctx: OpenAIChatCompletionsToolCodec.Context<fdm>);
6
+ encodeFunctionCall(fc: Function.Call.From<fdm>): OpenAI.ChatCompletionMessageToolCall;
7
+ decodeFunctionCall(apifc: OpenAI.ChatCompletionMessageFunctionToolCall): Function.Call.From<fdm>;
8
+ encodeFunctionResponse(fr: Function.Response.From<fdm>): OpenAI.ChatCompletionToolMessageParam;
9
+ protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): OpenAI.ChatCompletionTool;
10
+ encodeFunctionDeclarationMap(fdm: fdm): OpenAI.ChatCompletionTool[];
11
+ }
12
+ export declare namespace OpenAIChatCompletionsToolCodec {
13
+ interface Context<in out fdm extends Function.Decl.Map.Proto> {
14
+ parallelToolCall: boolean;
15
+ fdm: fdm;
16
+ }
17
+ }
@@ -0,0 +1,80 @@
1
+ import { Function } from "../../function.js";
2
+ import { ResponseInvalid } from "../../engine.js";
3
+ import Ajv from 'ajv';
4
+ const ajv = new Ajv();
5
+ export class OpenAIChatCompletionsToolCodec {
6
+ ctx;
7
+ constructor(ctx) {
8
+ this.ctx = ctx;
9
+ }
10
+ encodeFunctionCall(fc) {
11
+ if (fc.id) { }
12
+ else
13
+ throw new Error();
14
+ return {
15
+ id: fc.id,
16
+ type: 'function',
17
+ function: {
18
+ name: fc.name,
19
+ arguments: JSON.stringify(fc.args),
20
+ },
21
+ };
22
+ }
23
+ decodeFunctionCall(apifc) {
24
+ const fditem = this.ctx.fdm[apifc.function.name];
25
+ if (fditem) { }
26
+ else
27
+ throw new ResponseInvalid('Unknown function call', { cause: apifc });
28
+ const args = (() => {
29
+ try {
30
+ return JSON.parse(apifc.function.arguments);
31
+ }
32
+ catch (e) {
33
+ throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
34
+ }
35
+ })();
36
+ if (ajv.validate(fditem.parameters, args)) { }
37
+ else
38
+ throw new ResponseInvalid('Invalid function arguments', { cause: apifc });
39
+ return Function.Call.of({
40
+ id: apifc.id,
41
+ name: apifc.function.name,
42
+ args,
43
+ });
44
+ }
45
+ encodeFunctionResponse(fr) {
46
+ if (fr.id) { }
47
+ else
48
+ throw new Error();
49
+ if (fr instanceof Function.Response.Successful)
50
+ return {
51
+ role: 'tool',
52
+ tool_call_id: fr.id,
53
+ content: fr.text,
54
+ };
55
+ else if (fr instanceof Function.Response.Failed)
56
+ return {
57
+ role: 'tool',
58
+ tool_call_id: fr.id,
59
+ content: fr.error,
60
+ };
61
+ else
62
+ throw new Error();
63
+ }
64
+ encodeFunctionDeclarationEntry(fdentry) {
65
+ return {
66
+ type: 'function',
67
+ function: {
68
+ name: fdentry[0],
69
+ description: fdentry[1].description,
70
+ strict: true,
71
+ parameters: fdentry[1].parameters,
72
+ },
73
+ };
74
+ }
75
+ encodeFunctionDeclarationMap(fdm) {
76
+ const fdentries = Object.entries(fdm);
77
+ return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
78
+ }
79
+ }
80
+ //# sourceMappingURL=tool-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-codec.js","sourceRoot":"","sources":["../../../src/api-types/openai-chatcompletions/tool-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAGtB,MAAM,OAAO,8BAA8B;IACV;IAA7B,YAA6B,GAAgD;QAAhD,QAAG,GAAH,GAAG,CAA6C;IAAG,CAAC;IAG1E,kBAAkB,CACrB,EAA2B;QAE3B,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACrC,OAAO;YACH,EAAE,EAAE,EAAE,CAAC,EAAE;YACT,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACN,IAAI,EAAE,EAAE,CAAC,IAAI;gBACb,SAAS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;gBACZ,YAAY,EAAE,EAAE,CAAC,EAAE;gBACnB,OAAO,EAAE,EAAE,CAAC,IAAI;aACnB,CAAC;aACD,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,MAAM;YAC3C,OAAO;gBACH,IAAI,EAAE,MAAM;gBACZ,YAAY,EAAE,EAAE,CAAC,EAAE;gBACnB,OAAO,EAAE,EAAE,CAAC,KAAK;aACpB,CAAC;;YACD,MAAM,IAAI,KAAK,EAAE,CAAC;IAC3B,CAAC;IAES,8BAA8B,CACpC,OAAsC;QAEtC,OAAO;YACH,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACN,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;gBAChB,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW;gBACnC,MAAM,EAAE,IAAI;gBACZ,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,UAAU;aACpC;SACJ,CAAC;IACN,CAAC;IAEM,4BAA4B,CAAC,GAAQ;QACxC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAoC,CAAC;QACzE,OAAO,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC,CAAC;IAClF,CAAC;CAGJ"}
@@ -0,0 +1,12 @@
1
+ import OpenAI from 'openai';
2
+ import type { Pricing } from '../../engine.ts';
3
+ export declare class Billing {
4
+ protected ctx: Billing.Context;
5
+ constructor(ctx: Billing.Context);
6
+ charge(usage: OpenAI.Responses.ResponseUsage): 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.input_tokens_details.cached_tokens;
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/openai-responses/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;IACa;IAA7B,YAA6B,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAG9C,MAAM,CAAC,KAAqC;QAC/C,MAAM,kBAAkB,GAAG,KAAK,CAAC,oBAAoB,CAAC,aAAa,CAAC;QACpE,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 OpenAI from 'openai';
2
+ import { Function } from '../../function.ts';
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
+ encodeFunctionResponse(fr: Function.Response.From<fdm>): OpenAI.Responses.ResponseInputItem.FunctionCallOutput;
7
+ protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): OpenAI.Responses.FunctionTool;
8
+ encodeFunctionDeclarationMap(fdm: fdm): OpenAI.Responses.FunctionTool[];
9
+ decodeFunctionCall(apifc: OpenAI.Responses.ResponseFunctionToolCall): Function.Call.From<fdm>;
10
+ encodeFunctionCall(fc: Function.Call.From<fdm>): OpenAI.Responses.ResponseFunctionToolCall;
11
+ }
12
+ export declare namespace ToolCodec {
13
+ interface Context<in out fdm extends Function.Decl.Map.Proto> {
14
+ fdm: fdm;
15
+ }
16
+ }
@@ -0,0 +1,76 @@
1
+ import { Function } from "../../function.js";
2
+ import { ResponseInvalid } from "../../engine.js";
3
+ import Ajv from 'ajv';
4
+ const ajv = new Ajv();
5
+ export class ToolCodec {
6
+ ctx;
7
+ constructor(ctx) {
8
+ this.ctx = ctx;
9
+ }
10
+ encodeFunctionResponse(fr) {
11
+ if (fr.id) { }
12
+ else
13
+ throw new Error();
14
+ if (fr instanceof Function.Response.Successful)
15
+ return {
16
+ type: 'function_call_output',
17
+ call_id: fr.id,
18
+ output: fr.text,
19
+ };
20
+ else if (fr instanceof Function.Response.Failed)
21
+ return {
22
+ type: 'function_call_output',
23
+ call_id: fr.id,
24
+ output: fr.error,
25
+ };
26
+ else
27
+ throw new Error();
28
+ }
29
+ encodeFunctionDeclarationEntry(fdentry) {
30
+ return {
31
+ name: fdentry[0],
32
+ description: fdentry[1].description,
33
+ parameters: fdentry[1].parameters,
34
+ strict: true,
35
+ type: 'function',
36
+ };
37
+ }
38
+ encodeFunctionDeclarationMap(fdm) {
39
+ const fdentries = Object.entries(fdm);
40
+ return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
41
+ }
42
+ decodeFunctionCall(apifc) {
43
+ const fditem = this.ctx.fdm[apifc.name];
44
+ if (fditem) { }
45
+ else
46
+ throw new ResponseInvalid('Unknown function call', { cause: apifc });
47
+ const args = (() => {
48
+ try {
49
+ return JSON.parse(apifc.arguments);
50
+ }
51
+ catch (e) {
52
+ throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
53
+ }
54
+ })();
55
+ if (ajv.validate(fditem.parameters, args)) { }
56
+ else
57
+ throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
58
+ return Function.Call.of({
59
+ id: apifc.call_id,
60
+ name: apifc.name,
61
+ args,
62
+ });
63
+ }
64
+ encodeFunctionCall(fc) {
65
+ if (fc.id) { }
66
+ else
67
+ throw new Error();
68
+ return {
69
+ type: 'function_call',
70
+ call_id: fc.id,
71
+ name: fc.name,
72
+ arguments: JSON.stringify(fc.args),
73
+ };
74
+ }
75
+ }
76
+ //# sourceMappingURL=tool-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-codec.js","sourceRoot":"","sources":["../../../src/api-types/openai-responses/tool-codec.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAItB,MAAM,OAAO,SAAS;IAGW;IAA7B,YAA6B,GAA2B;QAA3B,QAAG,GAAH,GAAG,CAAwB;IAAG,CAAC;IAErD,sBAAsB,CACzB,EAA+B;QAE/B,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACrC,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,UAAU;YAC1C,OAAO;gBACH,IAAI,EAAE,sBAAsB;gBAC5B,OAAO,EAAE,EAAE,CAAC,EAAE;gBACd,MAAM,EAAE,EAAE,CAAC,IAAI;aAClB,CAAC;aACD,IAAI,EAAE,YAAY,QAAQ,CAAC,QAAQ,CAAC,MAAM;YAC3C,OAAO;gBACH,IAAI,EAAE,sBAAsB;gBAC5B,OAAO,EAAE,EAAE,CAAC,EAAE;gBACd,MAAM,EAAE,EAAE,CAAC,KAAK;aACnB,CAAC;;YACD,MAAM,IAAI,KAAK,EAAE,CAAC;IAC3B,CAAC;IAES,8BAA8B,CACpC,OAAsC;QAEtC,OAAO;YACH,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AK,CAAC,OAAO;YACjB,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI;SAC4B,CAAC,CAAC;IAC1C,CAAC;IAEM,kBAAkB,CACrB,EAA2B;QAE3B,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACrC,OAAO;YACH,IAAI,EAAE,eAAe;YACrB,OAAO,EAAE,EAAE,CAAC,EAAE;YACd,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC;SACrC,CAAC;IACN,CAAC;CACJ"}
@@ -0,0 +1,7 @@
1
+ declare const _default: {
2
+ readonly verbatim: {
3
+ readonly instruction: string;
4
+ readonly declarations: string;
5
+ };
6
+ };
7
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import { loadtext } from '@zimtsui/node-loaders';
2
+ export default {
3
+ verbatim: {
4
+ instruction: loadtext(import.meta.resolve('../assets/verbatim/instruction.md')),
5
+ declarations: loadtext(import.meta.resolve('../assets/verbatim/declarations.handlebars')),
6
+ },
7
+ };
8
+ //# sourceMappingURL=assets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assets.js","sourceRoot":"","sources":["../src/assets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAGjD,eAAe;IACX,QAAQ,EAAE;QACN,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,mCAAmC,CAAC,CAAC;QAC/E,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,4CAA4C,CAAC,CAAC;KAC5F;CACK,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { Function } from '../function.ts';
2
+ import { Engine } from '../engine.ts';
3
+ import { Verbatim } from '../verbatim.ts';
4
+ declare const NOMINAL: unique symbol;
5
+ export interface Session<in out fdu extends Function.Decl.Proto, in out vdu extends Verbatim.Decl.Proto> extends Engine.Session<RoleMessage.User<fdu>, RoleMessage.Ai<fdu, vdu>, RoleMessage.Developer> {
6
+ }
7
+ export declare namespace Session {
8
+ type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = Session<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
9
+ type ChatMessage<fdu extends Function.Decl.Proto, vdu extends Verbatim.Decl.Proto> = Engine.Session.ChatMessage<RoleMessage.User<fdu>, RoleMessage.Ai<fdu, vdu>>;
10
+ namespace ChatMessage {
11
+ type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = ChatMessage<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
12
+ }
13
+ }
14
+ export declare namespace RoleMessage {
15
+ namespace Part {
16
+ class Text<out vdu extends Verbatim.Decl.Proto> {
17
+ text: string;
18
+ vrs: Verbatim.Request.Of<vdu>[];
19
+ static paragraph(text: string): Text<never>;
20
+ protected [NOMINAL]: never;
21
+ constructor(text: string, vrs: Verbatim.Request.Of<vdu>[]);
22
+ }
23
+ }
24
+ class Ai<out fdu extends Function.Decl.Proto, out vdu extends Verbatim.Decl.Proto> {
25
+ protected parts: RoleMessage.Ai.Part<fdu, vdu>[];
26
+ protected [NOMINAL]: never;
27
+ constructor(parts: RoleMessage.Ai.Part<fdu, vdu>[]);
28
+ getParts(): RoleMessage.Ai.Part<fdu, vdu>[];
29
+ allTextPart(): boolean;
30
+ getTextParts(): RoleMessage.Part.Text<vdu>[];
31
+ getText(): string;
32
+ getFunctionCalls(): Function.Call.Of<fdu>[];
33
+ getVerbatimRequests(): Verbatim.Request.Of<vdu>[];
34
+ getOnlyFunctionCall(): Function.Call.Of<fdu>;
35
+ getOnlyVerbatimRequest(): Verbatim.Request.Of<vdu>;
36
+ }
37
+ namespace Ai {
38
+ type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = RoleMessage.Ai<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
39
+ type Part<fdu extends Function.Decl.Proto, vdu extends Verbatim.Decl.Proto> = RoleMessage.Part.Text<vdu> | Function.Call.Of<fdu>;
40
+ namespace Part {
41
+ type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = RoleMessage.Ai.Part<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
42
+ }
43
+ }
44
+ class User<out fdu extends Function.Decl.Proto> {
45
+ protected parts: RoleMessage.User.Part<fdu>[];
46
+ protected [NOMINAL]: never;
47
+ constructor(parts: RoleMessage.User.Part<fdu>[]);
48
+ getParts(): RoleMessage.User.Part<fdu>[];
49
+ getFunctionResponses(): Function.Response.Of<fdu>[];
50
+ getOnlyFunctionResponse(): Function.Response.Of<fdu>;
51
+ }
52
+ namespace User {
53
+ type From<fdm extends Function.Decl.Map.Proto> = RoleMessage.User<Function.Decl.From<fdm>>;
54
+ type Part<fdu extends Function.Decl.Proto> = RoleMessage.Part.Text<never> | Function.Response.Of<fdu>;
55
+ }
56
+ class Developer {
57
+ protected parts: RoleMessage.Developer.Part[];
58
+ protected [NOMINAL]: never;
59
+ constructor(parts: RoleMessage.Developer.Part[]);
60
+ getParts(): RoleMessage.Developer.Part[];
61
+ getText(): string;
62
+ }
63
+ namespace Developer {
64
+ type Part = RoleMessage.Part.Text<never>;
65
+ }
66
+ }
67
+ export {};