@sjts/ai-shared 31.1.2 → 31.2.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 (192) hide show
  1. package/backend/LICENCE +5 -0
  2. package/backend/dist/_/amazon/_/getAmazonClient.d.ts +4 -0
  3. package/backend/dist/_/amazon/_/getAmazonClient.d.ts.map +1 -0
  4. package/backend/dist/_/amazon/_/getAmazonClient.js +10 -0
  5. package/backend/dist/_/amazon/_/index.d.ts +2 -0
  6. package/backend/dist/_/amazon/_/index.d.ts.map +1 -0
  7. package/backend/dist/_/amazon/_/index.js +1 -0
  8. package/backend/dist/_/amazon/index.d.ts +2 -0
  9. package/backend/dist/_/amazon/index.d.ts.map +1 -0
  10. package/backend/dist/_/amazon/index.js +1 -0
  11. package/backend/dist/_/amazon/makeAmazonQuickie.d.ts +3 -0
  12. package/backend/dist/_/amazon/makeAmazonQuickie.d.ts.map +1 -0
  13. package/backend/dist/_/amazon/makeAmazonQuickie.js +65 -0
  14. package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.d.ts +4 -0
  15. package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.d.ts.map +1 -0
  16. package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.js +59 -0
  17. package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.d.ts +4 -0
  18. package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.d.ts.map +1 -0
  19. package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.js +11 -0
  20. package/backend/dist/_/anthropic/_/compileAnthropicThought.d.ts +7 -0
  21. package/backend/dist/_/anthropic/_/compileAnthropicThought.d.ts.map +1 -0
  22. package/backend/dist/_/anthropic/_/compileAnthropicThought.js +17 -0
  23. package/backend/dist/_/anthropic/_/getAnthropicClient.d.ts +4 -0
  24. package/backend/dist/_/anthropic/_/getAnthropicClient.d.ts.map +1 -0
  25. package/backend/dist/_/anthropic/_/getAnthropicClient.js +13 -0
  26. package/backend/dist/_/anthropic/_/index.d.ts +5 -0
  27. package/backend/dist/_/anthropic/_/index.d.ts.map +1 -0
  28. package/backend/dist/_/anthropic/_/index.js +4 -0
  29. package/backend/dist/_/anthropic/index.d.ts +4 -0
  30. package/backend/dist/_/anthropic/index.d.ts.map +1 -0
  31. package/backend/dist/_/anthropic/index.js +3 -0
  32. package/backend/dist/_/anthropic/makeAnthropicQuickie.d.ts +3 -0
  33. package/backend/dist/_/anthropic/makeAnthropicQuickie.d.ts.map +1 -0
  34. package/backend/dist/_/anthropic/makeAnthropicQuickie.js +53 -0
  35. package/backend/dist/_/anthropic/makeAnthropicStream.d.ts +3 -0
  36. package/backend/dist/_/anthropic/makeAnthropicStream.d.ts.map +1 -0
  37. package/backend/dist/_/anthropic/makeAnthropicStream.js +35 -0
  38. package/backend/dist/_/anthropic/makeAnthropicThink.d.ts +3 -0
  39. package/backend/dist/_/anthropic/makeAnthropicThink.d.ts.map +1 -0
  40. package/backend/dist/_/anthropic/makeAnthropicThink.js +84 -0
  41. package/backend/dist/_/callAI.d.ts +2 -0
  42. package/backend/dist/_/callAI.d.ts.map +1 -0
  43. package/backend/dist/_/callAI.js +10 -0
  44. package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.d.ts +3 -0
  45. package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.d.ts.map +1 -0
  46. package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.js +47 -0
  47. package/backend/dist/_/cerebras/_/getCerebrasClient.d.ts +4 -0
  48. package/backend/dist/_/cerebras/_/getCerebrasClient.d.ts.map +1 -0
  49. package/backend/dist/_/cerebras/_/getCerebrasClient.js +8 -0
  50. package/backend/dist/_/cerebras/_/index.d.ts +3 -0
  51. package/backend/dist/_/cerebras/_/index.d.ts.map +1 -0
  52. package/backend/dist/_/cerebras/_/index.js +2 -0
  53. package/backend/dist/_/cerebras/index.d.ts +3 -0
  54. package/backend/dist/_/cerebras/index.d.ts.map +1 -0
  55. package/backend/dist/_/cerebras/index.js +2 -0
  56. package/backend/dist/_/cerebras/makeCerebrasQuickie.d.ts +3 -0
  57. package/backend/dist/_/cerebras/makeCerebrasQuickie.d.ts.map +1 -0
  58. package/backend/dist/_/cerebras/makeCerebrasQuickie.js +44 -0
  59. package/backend/dist/_/cerebras/makeCerebrasThink.d.ts +3 -0
  60. package/backend/dist/_/cerebras/makeCerebrasThink.d.ts.map +1 -0
  61. package/backend/dist/_/cerebras/makeCerebrasThink.js +32 -0
  62. package/backend/dist/_/compileObject.d.ts +12 -0
  63. package/backend/dist/_/compileObject.d.ts.map +1 -0
  64. package/backend/dist/_/compileObject.js +13 -0
  65. package/backend/dist/_/compileParam.d.ts +4 -0
  66. package/backend/dist/_/compileParam.d.ts.map +1 -0
  67. package/backend/dist/_/compileParam.js +80 -0
  68. package/backend/dist/_/google/_/aiEvent2GoogleContent.d.ts +4 -0
  69. package/backend/dist/_/google/_/aiEvent2GoogleContent.d.ts.map +1 -0
  70. package/backend/dist/_/google/_/aiEvent2GoogleContent.js +56 -0
  71. package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.d.ts +4 -0
  72. package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.d.ts.map +1 -0
  73. package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.js +12 -0
  74. package/backend/dist/_/google/_/compileGoogleThought.d.ts +8 -0
  75. package/backend/dist/_/google/_/compileGoogleThought.d.ts.map +1 -0
  76. package/backend/dist/_/google/_/compileGoogleThought.js +22 -0
  77. package/backend/dist/_/google/_/getGoogleClient.d.ts +4 -0
  78. package/backend/dist/_/google/_/getGoogleClient.d.ts.map +1 -0
  79. package/backend/dist/_/google/_/getGoogleClient.js +8 -0
  80. package/backend/dist/_/google/_/getGoogleToolMode.d.ts +3 -0
  81. package/backend/dist/_/google/_/getGoogleToolMode.d.ts.map +1 -0
  82. package/backend/dist/_/google/_/getGoogleToolMode.js +11 -0
  83. package/backend/dist/_/google/_/index.d.ts +6 -0
  84. package/backend/dist/_/google/_/index.d.ts.map +1 -0
  85. package/backend/dist/_/google/_/index.js +5 -0
  86. package/backend/dist/_/google/index.d.ts +3 -0
  87. package/backend/dist/_/google/index.d.ts.map +1 -0
  88. package/backend/dist/_/google/index.js +2 -0
  89. package/backend/dist/_/google/makeGoogleQuickie.d.ts +3 -0
  90. package/backend/dist/_/google/makeGoogleQuickie.d.ts.map +1 -0
  91. package/backend/dist/_/google/makeGoogleQuickie.js +52 -0
  92. package/backend/dist/_/google/makeGoogleThink.d.ts +3 -0
  93. package/backend/dist/_/google/makeGoogleThink.d.ts.map +1 -0
  94. package/backend/dist/_/google/makeGoogleThink.js +93 -0
  95. package/backend/dist/_/index.d.ts +13 -0
  96. package/backend/dist/_/index.d.ts.map +1 -0
  97. package/backend/dist/_/index.js +12 -0
  98. package/backend/dist/_/logAI.d.ts +3 -0
  99. package/backend/dist/_/logAI.d.ts.map +1 -0
  100. package/backend/dist/_/logAI.js +7 -0
  101. package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.d.ts +3 -0
  102. package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.d.ts.map +1 -0
  103. package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.js +47 -0
  104. package/backend/dist/_/mistral/_/aiTool2MistralAITool.d.ts +3 -0
  105. package/backend/dist/_/mistral/_/aiTool2MistralAITool.d.ts.map +1 -0
  106. package/backend/dist/_/mistral/_/aiTool2MistralAITool.js +14 -0
  107. package/backend/dist/_/mistral/_/compileMistralAIThought.d.ts +6 -0
  108. package/backend/dist/_/mistral/_/compileMistralAIThought.d.ts.map +1 -0
  109. package/backend/dist/_/mistral/_/compileMistralAIThought.js +15 -0
  110. package/backend/dist/_/mistral/_/getMistralAIClient.d.ts +4 -0
  111. package/backend/dist/_/mistral/_/getMistralAIClient.d.ts.map +1 -0
  112. package/backend/dist/_/mistral/_/getMistralAIClient.js +8 -0
  113. package/backend/dist/_/mistral/_/index.d.ts +5 -0
  114. package/backend/dist/_/mistral/_/index.d.ts.map +1 -0
  115. package/backend/dist/_/mistral/_/index.js +4 -0
  116. package/backend/dist/_/mistral/index.d.ts +3 -0
  117. package/backend/dist/_/mistral/index.d.ts.map +1 -0
  118. package/backend/dist/_/mistral/index.js +2 -0
  119. package/backend/dist/_/mistral/makeMistralAIQuickie.d.ts +3 -0
  120. package/backend/dist/_/mistral/makeMistralAIQuickie.d.ts.map +1 -0
  121. package/backend/dist/_/mistral/makeMistralAIQuickie.js +45 -0
  122. package/backend/dist/_/mistral/makeMistralAIThink.d.ts +3 -0
  123. package/backend/dist/_/mistral/makeMistralAIThink.d.ts.map +1 -0
  124. package/backend/dist/_/mistral/makeMistralAIThink.js +74 -0
  125. package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.d.ts +4 -0
  126. package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.d.ts.map +1 -0
  127. package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.js +47 -0
  128. package/backend/dist/_/openai/_/aiTool2OpenAITool.d.ts +4 -0
  129. package/backend/dist/_/openai/_/aiTool2OpenAITool.d.ts.map +1 -0
  130. package/backend/dist/_/openai/_/aiTool2OpenAITool.js +14 -0
  131. package/backend/dist/_/openai/_/compileOpenAIThought.d.ts +7 -0
  132. package/backend/dist/_/openai/_/compileOpenAIThought.d.ts.map +1 -0
  133. package/backend/dist/_/openai/_/compileOpenAIThought.js +17 -0
  134. package/backend/dist/_/openai/_/getOpenAIClient.d.ts +4 -0
  135. package/backend/dist/_/openai/_/getOpenAIClient.d.ts.map +1 -0
  136. package/backend/dist/_/openai/_/getOpenAIClient.js +12 -0
  137. package/backend/dist/_/openai/_/index.d.ts +5 -0
  138. package/backend/dist/_/openai/_/index.d.ts.map +1 -0
  139. package/backend/dist/_/openai/_/index.js +4 -0
  140. package/backend/dist/_/openai/index.d.ts +4 -0
  141. package/backend/dist/_/openai/index.d.ts.map +1 -0
  142. package/backend/dist/_/openai/index.js +3 -0
  143. package/backend/dist/_/openai/makeOpenAIEmbed.d.ts +3 -0
  144. package/backend/dist/_/openai/makeOpenAIEmbed.d.ts.map +1 -0
  145. package/backend/dist/_/openai/makeOpenAIEmbed.js +15 -0
  146. package/backend/dist/_/openai/makeOpenAIQuickie.d.ts +3 -0
  147. package/backend/dist/_/openai/makeOpenAIQuickie.d.ts.map +1 -0
  148. package/backend/dist/_/openai/makeOpenAIQuickie.js +47 -0
  149. package/backend/dist/_/openai/makeOpenAIThink.d.ts +3 -0
  150. package/backend/dist/_/openai/makeOpenAIThink.d.ts.map +1 -0
  151. package/backend/dist/_/openai/makeOpenAIThink.js +75 -0
  152. package/backend/dist/_/types/errors.d.ts +4 -0
  153. package/backend/dist/_/types/errors.d.ts.map +1 -0
  154. package/backend/dist/_/types/errors.js +3 -0
  155. package/backend/dist/_/types/index.d.ts +2 -0
  156. package/backend/dist/_/types/index.d.ts.map +1 -0
  157. package/backend/dist/_/types/index.js +1 -0
  158. package/backend/dist/_/voyageai/_/getVoyageAIClient.d.ts +4 -0
  159. package/backend/dist/_/voyageai/_/getVoyageAIClient.d.ts.map +1 -0
  160. package/backend/dist/_/voyageai/_/getVoyageAIClient.js +7 -0
  161. package/backend/dist/_/voyageai/_/index.d.ts +2 -0
  162. package/backend/dist/_/voyageai/_/index.d.ts.map +1 -0
  163. package/backend/dist/_/voyageai/_/index.js +1 -0
  164. package/backend/dist/_/voyageai/index.d.ts +2 -0
  165. package/backend/dist/_/voyageai/index.d.ts.map +1 -0
  166. package/backend/dist/_/voyageai/index.js +1 -0
  167. package/backend/dist/_/voyageai/makeVoyageAIEmbed.d.ts +3 -0
  168. package/backend/dist/_/voyageai/makeVoyageAIEmbed.d.ts.map +1 -0
  169. package/backend/dist/_/voyageai/makeVoyageAIEmbed.js +13 -0
  170. package/backend/dist/doEmbed.d.ts +5 -0
  171. package/backend/dist/doEmbed.d.ts.map +1 -0
  172. package/backend/dist/doEmbed.js +17 -0
  173. package/backend/dist/doQuickie.d.ts +5 -0
  174. package/backend/dist/doQuickie.d.ts.map +1 -0
  175. package/backend/dist/doQuickie.js +39 -0
  176. package/backend/dist/doStream.d.ts +5 -0
  177. package/backend/dist/doStream.d.ts.map +1 -0
  178. package/backend/dist/doStream.js +14 -0
  179. package/backend/dist/doThink.d.ts +4 -0
  180. package/backend/dist/doThink.d.ts.map +1 -0
  181. package/backend/dist/doThink.js +33 -0
  182. package/backend/dist/index.d.ts +5 -0
  183. package/backend/dist/index.d.ts.map +1 -0
  184. package/backend/dist/index.js +4 -0
  185. package/backend/package.json +46 -0
  186. package/dist/utils/indent.d.ts +1 -1
  187. package/dist/utils/indent.d.ts.map +1 -1
  188. package/dist/utils/indent.js +2 -2
  189. package/dist/utils/trimdent.d.ts +1 -1
  190. package/dist/utils/trimdent.d.ts.map +1 -1
  191. package/dist/utils/trimdent.js +4 -3
  192. package/package.json +4 -4
@@ -0,0 +1,5 @@
1
+
2
+ Copyright (C) Mr Stephen James - All Rights Reserved
3
+ Unauthorized copying of any files within this source code repository, via any medium is strictly prohibited
4
+ Proprietary and confidential
5
+ Written by Steve James <steve@stevejames.dev>, March 2019
@@ -0,0 +1,4 @@
1
+ import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime';
2
+ import { APIConfig } from '@sjts/lib-utils';
3
+ export declare function getAmazonClient(cfg: APIConfig): BedrockRuntimeClient;
4
+ //# sourceMappingURL=getAmazonClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAmazonClient.d.ts","sourceRoot":"","sources":["../../../../src/_/amazon/_/getAmazonClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAA;AACtE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,eAAe,CAAC,GAAG,EAAE,SAAS,GAAG,oBAAoB,CAOpE"}
@@ -0,0 +1,10 @@
1
+ import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime';
2
+ const _cache = { current: undefined };
3
+ export function getAmazonClient(cfg) {
4
+ if (!_cache.current) {
5
+ _cache.current = new BedrockRuntimeClient({
6
+ region: cfg.customAPISettings?.region ?? 'ap-southeast-2',
7
+ });
8
+ }
9
+ return _cache.current;
10
+ }
@@ -0,0 +1,2 @@
1
+ export * from './getAmazonClient.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/amazon/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
@@ -0,0 +1 @@
1
+ export * from './getAmazonClient.js';
@@ -0,0 +1,2 @@
1
+ export * from './makeAmazonQuickie.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/amazon/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
@@ -0,0 +1 @@
1
+ export * from './makeAmazonQuickie.js';
@@ -0,0 +1,3 @@
1
+ import { AIConfig, AIImage } from '@sjts/ai-shared';
2
+ export declare function makeAmazonQuickie(): (ai: AIConfig, content: string, images?: AIImage[]) => Promise<string>;
3
+ //# sourceMappingURL=makeAmazonQuickie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeAmazonQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/amazon/makeAmazonQuickie.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAIpE,wBAAgB,iBAAiB,KAClB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
@@ -0,0 +1,65 @@
1
+ import { ConversationRole, ConverseCommand } from '@aws-sdk/client-bedrock-runtime';
2
+ import { defaultAIConfig } from '@sjts/ai-shared';
3
+ import { Logger } from '@sjts/lib-log';
4
+ import { getAmazonClient } from './_/index.js';
5
+ export function makeAmazonQuickie() {
6
+ return async (ai, content, images = []) => {
7
+ const response = await handleRequest(ai, content, images);
8
+ return handleResponse(response);
9
+ };
10
+ }
11
+ // ---------------
12
+ // HANDLE REQUEST
13
+ // ---------------
14
+ async function handleRequest(ai, content, images = []) {
15
+ const client = getAmazonClient(ai.api);
16
+ const c = { ...defaultAIConfig, ...ai };
17
+ const generationConfig = {
18
+ maxOutputTokens: c.max_tokens,
19
+ };
20
+ const payload = {
21
+ textGenerationConfig: {
22
+ maxTokenCount: c.max_tokens,
23
+ stopSequences: [],
24
+ temperature: 0.7,
25
+ topP: 1,
26
+ },
27
+ };
28
+ // Step 4: Send and process the request
29
+ // - Embed the payload in a request object
30
+ // - Send the request to the model
31
+ // - Extract and return the generated image data from the response
32
+ try {
33
+ const request = {
34
+ modelId: c.llm.model,
35
+ messages: [
36
+ {
37
+ content: [{ text: content }],
38
+ role: ConversationRole.USER,
39
+ },
40
+ ],
41
+ inferenceConfig: {
42
+ maxTokens: c.max_tokens, // The maximum response length
43
+ temperature: 0.5, // Using temperature for randomness control
44
+ //topP: 0.9, // Alternative: use topP instead of temperature
45
+ },
46
+ };
47
+ return client.send(new ConverseCommand(request));
48
+ }
49
+ catch (e) {
50
+ Logger.error('Problem processing AI Request', { message: e.message });
51
+ throw e;
52
+ }
53
+ }
54
+ // ---------------
55
+ // HANDLE RESPONSE
56
+ // ---------------
57
+ async function handleResponse(response) {
58
+ try {
59
+ return response.output.message.content[0].text;
60
+ }
61
+ catch (e) {
62
+ Logger.error('Problem processing AI response', { message: e.message });
63
+ throw e;
64
+ }
65
+ }
@@ -0,0 +1,4 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ import { AIEvent } from '@sjts/ai-shared';
3
+ export declare function aiEvent2AnthropicMessage(message: AIEvent): Anthropic.Beta.Tools.Messages.ToolsBetaMessageParam;
4
+ //# sourceMappingURL=aiEvent2AnthropicMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiEvent2AnthropicMessage.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/aiEvent2AnthropicMessage.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,OAAO,EAAe,MAAM,iBAAiB,CAAA;AAEtD,wBAAgB,wBAAwB,CACvC,OAAO,EAAE,OAAO,GACd,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,qBAAqB,CAkErD"}
@@ -0,0 +1,59 @@
1
+ import { AIEventType } from '@sjts/ai-shared';
2
+ export function aiEvent2AnthropicMessage(message) {
3
+ switch (message.type) {
4
+ case AIEventType.USER_CHAT:
5
+ // Include Images
6
+ if (message.images?.length) {
7
+ return {
8
+ role: 'user',
9
+ content: [
10
+ { type: 'text', text: message.content },
11
+ ...message.images.map(image => ({
12
+ type: 'image',
13
+ source: {
14
+ type: 'base64',
15
+ media_type: image.mime,
16
+ data: image.data,
17
+ },
18
+ })),
19
+ ],
20
+ };
21
+ }
22
+ // Plain Text
23
+ return {
24
+ role: 'user',
25
+ content: [{ type: 'text', text: message.content }],
26
+ };
27
+ case AIEventType.AI_CHAT:
28
+ return {
29
+ role: 'assistant',
30
+ content: [{ type: 'text', text: message.content }],
31
+ };
32
+ case AIEventType.TOOL_REQUEST:
33
+ return {
34
+ role: 'assistant',
35
+ content: [
36
+ message.content
37
+ ? { type: 'text', text: message.content }
38
+ : undefined,
39
+ ...message.toolCalls.map(action => ({
40
+ type: 'tool_use',
41
+ id: action.callId,
42
+ name: action.tool,
43
+ input: action.params,
44
+ })),
45
+ ].filter(Boolean),
46
+ };
47
+ case AIEventType.TOOL_RESPONSE:
48
+ return {
49
+ role: 'user',
50
+ content: [
51
+ {
52
+ type: 'tool_result',
53
+ tool_use_id: message.callId,
54
+ content: [{ type: 'text', text: message.content }],
55
+ },
56
+ ],
57
+ };
58
+ }
59
+ }
@@ -0,0 +1,4 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ import { AITool } from '@sjts/ai-shared';
3
+ export declare function aiTool2AnthropicTool(tool: AITool): Anthropic.Beta.Tools.Messages.Tool;
4
+ //# sourceMappingURL=aiTool2AnthropicTool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiTool2AnthropicTool.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/aiTool2AnthropicTool.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAGxC,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CASrF"}
@@ -0,0 +1,11 @@
1
+ import { compileObject } from '../../compileObject.js';
2
+ export function aiTool2AnthropicTool(tool) {
3
+ return {
4
+ name: tool.key,
5
+ description: tool.description,
6
+ input_schema: {
7
+ ...compileObject(tool.params),
8
+ type: 'object',
9
+ },
10
+ };
11
+ }
@@ -0,0 +1,7 @@
1
+ import { AIThoughtInput } from '@sjts/ai-shared';
2
+ export declare function compileAnthropicThought(input: AIThoughtInput): {
3
+ system: string | undefined;
4
+ messages: any[];
5
+ tools: any[] | undefined;
6
+ };
7
+ //# sourceMappingURL=compileAnthropicThought.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compileAnthropicThought.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/compileAnthropicThought.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,cAAc,GAAG;IAC/D,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,QAAQ,EAAE,GAAG,EAAE,CAAA;IACf,KAAK,EAAE,GAAG,EAAE,GAAG,SAAS,CAAA;CACxB,CAgBA"}
@@ -0,0 +1,17 @@
1
+ import { aiEvent2AnthropicMessage } from './aiEvent2AnthropicMessage.js';
2
+ import { aiTool2AnthropicTool } from './aiTool2AnthropicTool.js';
3
+ export function compileAnthropicThought(input) {
4
+ let system = input.context;
5
+ let messages = input.history?.map(aiEvent2AnthropicMessage) ?? [];
6
+ const tools = input.tools?.map(aiTool2AnthropicTool) ?? undefined;
7
+ if (!messages?.length && system) {
8
+ messages = [
9
+ {
10
+ role: 'user',
11
+ content: [{ type: 'text', text: system }],
12
+ },
13
+ ];
14
+ system = '';
15
+ }
16
+ return { system, messages, tools };
17
+ }
@@ -0,0 +1,4 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ import { APIConfig } from '@sjts/lib-utils';
3
+ export declare function getAnthropicClient(apiConfig: APIConfig): Anthropic;
4
+ //# sourceMappingURL=getAnthropicClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAnthropicClient.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/getAnthropicClient.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAUlE"}
@@ -0,0 +1,13 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ const _cache = { current: undefined };
3
+ export function getAnthropicClient(apiConfig) {
4
+ if (!_cache.current) {
5
+ _cache.current = new Anthropic({
6
+ apiKey: apiConfig.apiKey,
7
+ defaultHeaders: {
8
+ 'anthropic-beta': 'tools-2024-04-04',
9
+ },
10
+ });
11
+ }
12
+ return _cache.current;
13
+ }
@@ -0,0 +1,5 @@
1
+ export * from './aiEvent2AnthropicMessage.js';
2
+ export * from './aiTool2AnthropicTool.js';
3
+ export * from './compileAnthropicThought.js';
4
+ export * from './getAnthropicClient.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,yBAAyB,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './aiEvent2AnthropicMessage.js';
2
+ export * from './aiTool2AnthropicTool.js';
3
+ export * from './compileAnthropicThought.js';
4
+ export * from './getAnthropicClient.js';
@@ -0,0 +1,4 @@
1
+ export * from './makeAnthropicQuickie.js';
2
+ export * from './makeAnthropicStream.js';
3
+ export * from './makeAnthropicThink.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AACzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './makeAnthropicQuickie.js';
2
+ export * from './makeAnthropicStream.js';
3
+ export * from './makeAnthropicThink.js';
@@ -0,0 +1,3 @@
1
+ import { AIConfig, AIImage } from '@sjts/ai-shared';
2
+ export declare function makeAnthropicQuickie(): (ai: AIConfig, content: string, images?: AIImage[]) => Promise<string>;
3
+ //# sourceMappingURL=makeAnthropicQuickie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeAnthropicQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicQuickie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAMjF,wBAAgB,oBAAoB,KACrB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
@@ -0,0 +1,53 @@
1
+ import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
2
+ import { Logger } from '@sjts/lib-log';
3
+ import { callAI } from '../callAI.js';
4
+ import { getAnthropicClient } from './_/getAnthropicClient.js';
5
+ import { aiEvent2AnthropicMessage } from './_/index.js';
6
+ export function makeAnthropicQuickie() {
7
+ return async (ai, content, images = []) => {
8
+ const response = await handleRequest(ai, content, images);
9
+ return handleResponse(response);
10
+ };
11
+ }
12
+ // ---------------
13
+ // HANDLE REQUEST
14
+ // ---------------
15
+ async function handleRequest(ai, content, images = []) {
16
+ try {
17
+ const client = getAnthropicClient(ai.api);
18
+ const c = { ...defaultAIConfig, ...ai };
19
+ // Handling JSON mode
20
+ if (ai.jsonMode) {
21
+ content =
22
+ `# IMPORTANT
23
+ - Please respond in JSON
24
+ - Do not wrap the JSON in Markdown
25
+ - Raw JSON, starting with { and ending with }
26
+ - Thank you
27
+
28
+ ` + content;
29
+ }
30
+ const request = {
31
+ messages: [aiEvent2AnthropicMessage({ type: AIEventType.USER_CHAT, content, images })],
32
+ model: c.llm.model,
33
+ max_tokens: c.max_tokens,
34
+ };
35
+ return callAI(r => client.messages.create(r), request, c.timeout);
36
+ }
37
+ catch (e) {
38
+ Logger.error('Problem processing AI Request', { message: e.message });
39
+ throw e;
40
+ }
41
+ }
42
+ // ---------------
43
+ // HANDLE RESPONSE
44
+ // ---------------
45
+ async function handleResponse(response) {
46
+ try {
47
+ return response.content[0].text;
48
+ }
49
+ catch (e) {
50
+ Logger.error('Problem processing AI response', { message: e.message });
51
+ throw e;
52
+ }
53
+ }
@@ -0,0 +1,3 @@
1
+ import { AIConfig, AIImage } from '@sjts/ai-shared';
2
+ export declare function makeAnthropicStream(): (ai: AIConfig, content: string, images: AIImage[] | undefined, onChunk: (chunk: string) => void) => Promise<void>;
3
+ //# sourceMappingURL=makeAnthropicStream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeAnthropicStream.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAIjF,wBAAgB,mBAAmB,KAEjC,IAAI,QAAQ,EACZ,SAAS,MAAM,EACf,QAAQ,OAAO,EAAE,YAAK,EACtB,SAAS,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,KAC9B,OAAO,CAAC,IAAI,CAAC,CA+BhB"}
@@ -0,0 +1,35 @@
1
+ import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
2
+ import { getAnthropicClient } from './_/getAnthropicClient.js';
3
+ import { aiEvent2AnthropicMessage } from './_/index.js';
4
+ export function makeAnthropicStream() {
5
+ return async (ai, content, images = [], onChunk) => {
6
+ await new Promise(async (resolve, reject) => {
7
+ const client = getAnthropicClient(ai.api);
8
+ const c = { ...defaultAIConfig, ...ai };
9
+ await client.messages
10
+ .stream({
11
+ messages: [
12
+ aiEvent2AnthropicMessage({
13
+ type: AIEventType.USER_CHAT,
14
+ content,
15
+ images,
16
+ }),
17
+ ],
18
+ model: c.llm.model,
19
+ max_tokens: c.max_tokens,
20
+ })
21
+ .on('text', text => {
22
+ console.log('Stream text', text);
23
+ onChunk(text);
24
+ })
25
+ .on('error', error => {
26
+ console.log('Stream error', error);
27
+ reject(error);
28
+ })
29
+ .on('end', () => {
30
+ console.log('Stream end');
31
+ resolve();
32
+ });
33
+ });
34
+ };
35
+ }
@@ -0,0 +1,3 @@
1
+ import { AIConfig, AIThoughtInput, AIThoughtOutput } from '@sjts/ai-shared';
2
+ export declare function makeAnthropicThink(): (ai: AIConfig, input: AIThoughtInput) => Promise<AIThoughtOutput>;
3
+ //# sourceMappingURL=makeAnthropicThink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeAnthropicThink.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicThink.ts"],"names":[],"mappings":"AACA,OAAO,EACN,QAAQ,EAER,cAAc,EACd,eAAe,EAGf,MAAM,iBAAiB,CAAA;AAMxB,wBAAgB,kBAAkB,KACnB,IAAI,QAAQ,EAAE,OAAO,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC,CAI5E"}
@@ -0,0 +1,84 @@
1
+ import { AIEventType, AIToolChoice, defaultAIConfig, } from '@sjts/ai-shared';
2
+ import { Logger } from '@sjts/lib-log';
3
+ import { callAI } from '../callAI.js';
4
+ import { compileAnthropicThought } from './_/compileAnthropicThought.js';
5
+ import { getAnthropicClient } from './_/getAnthropicClient.js';
6
+ export function makeAnthropicThink() {
7
+ return async (ai, input) => {
8
+ const response = await handleRequest(ai, input);
9
+ return handleResponse(response);
10
+ };
11
+ }
12
+ // ---------------
13
+ // HANDLE REQUEST
14
+ // ---------------
15
+ async function handleRequest(ai, input) {
16
+ try {
17
+ const client = getAnthropicClient(ai.api);
18
+ let { system, messages, tools } = compileAnthropicThought(input);
19
+ const c = { ...defaultAIConfig, ...ai };
20
+ // Handling JSON mode
21
+ if (ai.jsonMode) {
22
+ system += `
23
+ # IMPORTANT
24
+ - Please respond in JSON
25
+ - Do not wrap the JSON in Markdown
26
+ - Raw JSON, starting with { and ending with }
27
+ - Thank you
28
+ `;
29
+ }
30
+ const request = {
31
+ system,
32
+ messages,
33
+ tools,
34
+ model: c.llm.model,
35
+ max_tokens: c.max_tokens,
36
+ };
37
+ // Handling REQUIRED tool choice
38
+ if (c.tool_choice === AIToolChoice.REQUIRED) {
39
+ request.tool_choice = { type: 'any' };
40
+ }
41
+ return callAI(r => client.messages.create(r), request, c.timeout);
42
+ }
43
+ catch (e) {
44
+ Logger.error('Problem processing AI Request', { message: e.message });
45
+ throw e;
46
+ }
47
+ }
48
+ // ---------------
49
+ // HANDLE RESPONSE
50
+ // ---------------
51
+ async function handleResponse(response) {
52
+ try {
53
+ const textBlock = response.content.find(c => c.type === 'text');
54
+ const toolBlocks = response.content.filter(c => c.type === 'tool_use');
55
+ //----------------
56
+ // AI TOOL CALL MESSAGE
57
+ //----------------
58
+ if (!!toolBlocks?.length) {
59
+ return {
60
+ type: AIEventType.TOOL_REQUEST,
61
+ content: textBlock?.text ?? undefined,
62
+ toolCalls: toolBlocks.map(tc => ({
63
+ callId: tc.id,
64
+ tool: tc.name,
65
+ params: tc.input,
66
+ })),
67
+ };
68
+ }
69
+ //----------------
70
+ // AI CHAT MESSAGE
71
+ //----------------
72
+ else {
73
+ return {
74
+ type: AIEventType.AI_CHAT,
75
+ content: textBlock.text ?? '',
76
+ };
77
+ }
78
+ }
79
+ catch (e) {
80
+ Logger.error('Problem processing AI response', { message: e.message });
81
+ Logger.error(JSON.stringify(response, null, 2));
82
+ throw e;
83
+ }
84
+ }
@@ -0,0 +1,2 @@
1
+ export declare function callAI(fnc: (request: any) => Promise<any>, request: any, timeout: number): Promise<any>;
2
+ //# sourceMappingURL=callAI.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callAI.d.ts","sourceRoot":"","sources":["../../src/_/callAI.ts"],"names":[],"mappings":"AAEA,wBAAsB,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,gBAO9F"}
@@ -0,0 +1,10 @@
1
+ import { AITimeoutError } from './types/errors.js';
2
+ export async function callAI(fnc, request, timeout) {
3
+ const response = await Promise.race([
4
+ fnc(request),
5
+ new Promise(r => setTimeout(() => r('timeout'), timeout)),
6
+ ]);
7
+ if (response === 'timeout')
8
+ throw new AITimeoutError();
9
+ return response;
10
+ }
@@ -0,0 +1,3 @@
1
+ import { AIEvent } from '@sjts/ai-shared';
2
+ export declare function aiEvent2CerebrasMessage(event: AIEvent): any;
3
+ //# sourceMappingURL=aiEvent2CerebrasMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiEvent2CerebrasMessage.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/aiEvent2CerebrasMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,iBAAiB,CAAA;AAEtD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,CAmD3D"}
@@ -0,0 +1,47 @@
1
+ import { AIEventType } from '@sjts/ai-shared';
2
+ export function aiEvent2CerebrasMessage(event) {
3
+ switch (event.type) {
4
+ case AIEventType.USER_CHAT:
5
+ // Include Images
6
+ if (event.images?.length) {
7
+ return {
8
+ role: 'user',
9
+ content: [
10
+ { type: 'text', text: event.content },
11
+ ...event.images.map(image => ({
12
+ type: 'image_url',
13
+ image_url: { url: `data:${image.mime};base64,${image.data}` },
14
+ })),
15
+ ],
16
+ };
17
+ }
18
+ // Plain Text
19
+ return {
20
+ role: 'user',
21
+ content: event.content,
22
+ };
23
+ case AIEventType.AI_CHAT:
24
+ return {
25
+ role: 'assistant',
26
+ content: event.content,
27
+ };
28
+ case AIEventType.TOOL_REQUEST:
29
+ return {
30
+ role: 'assistant',
31
+ tool_calls: event.toolCalls.map(tc => ({
32
+ id: tc.callId,
33
+ type: 'function',
34
+ function: {
35
+ name: tc.tool,
36
+ arguments: JSON.stringify(tc.params),
37
+ },
38
+ })),
39
+ };
40
+ case AIEventType.TOOL_RESPONSE:
41
+ return {
42
+ role: 'tool',
43
+ tool_call_id: event.callId,
44
+ content: event.content,
45
+ };
46
+ }
47
+ }
@@ -0,0 +1,4 @@
1
+ import Cerebras from '@cerebras/cerebras_cloud_sdk';
2
+ import { APIConfig } from '@sjts/lib-utils';
3
+ export declare function getCerebrasClient(cfg: APIConfig): Cerebras;
4
+ //# sourceMappingURL=getCerebrasClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getCerebrasClient.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/getCerebrasClient.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,8BAA8B,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,SAAS,GAAG,QAAQ,CAK1D"}
@@ -0,0 +1,8 @@
1
+ import Cerebras from '@cerebras/cerebras_cloud_sdk';
2
+ const _cache = { current: undefined };
3
+ export function getCerebrasClient(cfg) {
4
+ if (!_cache.current) {
5
+ _cache.current = new Cerebras({ apiKey: cfg.apiKey });
6
+ }
7
+ return _cache.current;
8
+ }
@@ -0,0 +1,3 @@
1
+ export * from './aiEvent2CerebrasMessage.js';
2
+ export * from './getCerebrasClient.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA;AAC5C,cAAc,wBAAwB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './aiEvent2CerebrasMessage.js';
2
+ export * from './getCerebrasClient.js';
@@ -0,0 +1,3 @@
1
+ export * from './makeCerebrasQuickie.js';
2
+ export * from './makeCerebrasThink.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/cerebras/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAA;AACxC,cAAc,wBAAwB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './makeCerebrasQuickie.js';
2
+ export * from './makeCerebrasThink.js';
@@ -0,0 +1,3 @@
1
+ import { AIConfig, AIImage } from '@sjts/ai-shared';
2
+ export declare function makeCerebrasQuickie(): (ai: AIConfig, content: string, images?: AIImage[]) => Promise<string>;
3
+ //# sourceMappingURL=makeCerebrasQuickie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeCerebrasQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/cerebras/makeCerebrasQuickie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAKjF,wBAAgB,mBAAmB,KACpB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}