@aigne/core 0.0.1

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 (170) hide show
  1. package/lib/cjs/assistant/generate-output.js +101 -0
  2. package/lib/cjs/assistant/select-agent.js +76 -0
  3. package/lib/cjs/assistant/type.js +11 -0
  4. package/lib/cjs/common/aid.js +42 -0
  5. package/lib/cjs/common/index.js +238 -0
  6. package/lib/cjs/common/resource-manager.js +199 -0
  7. package/lib/cjs/constants.js +9 -0
  8. package/lib/cjs/executor/agent.js +10 -0
  9. package/lib/cjs/executor/aigc.js +28 -0
  10. package/lib/cjs/executor/api.js +64 -0
  11. package/lib/cjs/executor/base.js +676 -0
  12. package/lib/cjs/executor/blocklet.js +25 -0
  13. package/lib/cjs/executor/call-agent.js +105 -0
  14. package/lib/cjs/executor/decision.js +478 -0
  15. package/lib/cjs/executor/image-blender.js +32 -0
  16. package/lib/cjs/executor/index.js +81 -0
  17. package/lib/cjs/executor/llm.js +379 -0
  18. package/lib/cjs/executor/logic.js +167 -0
  19. package/lib/cjs/index.js +17 -0
  20. package/lib/cjs/libs/blocklet/vc.js +92 -0
  21. package/lib/cjs/libs/openapi/request/index.js +24 -0
  22. package/lib/cjs/libs/openapi/request/util.js +146 -0
  23. package/lib/cjs/libs/openapi/types/index.js +17 -0
  24. package/lib/cjs/libs/openapi/util/call.js +15 -0
  25. package/lib/cjs/libs/openapi/util/check-schema.js +67 -0
  26. package/lib/cjs/libs/openapi/util/convert-schema.js +44 -0
  27. package/lib/cjs/libs/openapi/util/flatten-open-api.js +21 -0
  28. package/lib/cjs/libs/openapi/util/get-open-api-i18n-text.js +7 -0
  29. package/lib/cjs/logger.js +4 -0
  30. package/lib/cjs/runtime/resource-blocklet.js +5 -0
  31. package/lib/cjs/runtime/runtime.js +143 -0
  32. package/lib/cjs/types/assistant/index.js +31 -0
  33. package/lib/cjs/types/assistant/mustache/ReadableMustache.js +69 -0
  34. package/lib/cjs/types/assistant/mustache/directive.js +35 -0
  35. package/lib/cjs/types/assistant/mustache/mustache.js +688 -0
  36. package/lib/cjs/types/common/index.js +2 -0
  37. package/lib/cjs/types/index.js +20 -0
  38. package/lib/cjs/types/resource/index.js +47 -0
  39. package/lib/cjs/types/resource/project.js +35 -0
  40. package/lib/cjs/types/runtime/agent.js +2 -0
  41. package/lib/cjs/types/runtime/error.js +18 -0
  42. package/lib/cjs/types/runtime/index.js +37 -0
  43. package/lib/cjs/types/runtime/runtime-resource-blocklet-state.js +4 -0
  44. package/lib/cjs/types/runtime/schema.js +259 -0
  45. package/lib/cjs/utils/cron-job.js +48 -0
  46. package/lib/cjs/utils/extract-metadata-transform.js +58 -0
  47. package/lib/cjs/utils/extract-metadata-transform.test.js +61 -0
  48. package/lib/cjs/utils/fs.js +49 -0
  49. package/lib/cjs/utils/get-blocklet-agent.js +351 -0
  50. package/lib/cjs/utils/geti.js +37 -0
  51. package/lib/cjs/utils/is-non-nullable.js +20 -0
  52. package/lib/cjs/utils/render-message.js +23 -0
  53. package/lib/cjs/utils/resolve-secret-inputs.js +49 -0
  54. package/lib/cjs/utils/retry.js +19 -0
  55. package/lib/cjs/utils/task-id.js +7 -0
  56. package/lib/cjs/utils/tool-calls-transform.js +18 -0
  57. package/lib/esm/assistant/generate-output.js +91 -0
  58. package/lib/esm/assistant/select-agent.js +71 -0
  59. package/lib/esm/assistant/type.js +7 -0
  60. package/lib/esm/common/aid.js +38 -0
  61. package/lib/esm/common/index.js +232 -0
  62. package/lib/esm/common/resource-manager.js +192 -0
  63. package/lib/esm/constants.js +6 -0
  64. package/lib/esm/executor/agent.js +6 -0
  65. package/lib/esm/executor/aigc.js +24 -0
  66. package/lib/esm/executor/api.js +34 -0
  67. package/lib/esm/executor/base.js +668 -0
  68. package/lib/esm/executor/blocklet.js +21 -0
  69. package/lib/esm/executor/call-agent.js +98 -0
  70. package/lib/esm/executor/decision.js +471 -0
  71. package/lib/esm/executor/image-blender.js +25 -0
  72. package/lib/esm/executor/index.js +74 -0
  73. package/lib/esm/executor/llm.js +372 -0
  74. package/lib/esm/executor/logic.js +160 -0
  75. package/lib/esm/index.js +1 -0
  76. package/lib/esm/libs/blocklet/vc.js +85 -0
  77. package/lib/esm/libs/openapi/request/index.js +20 -0
  78. package/lib/esm/libs/openapi/request/util.js +136 -0
  79. package/lib/esm/libs/openapi/types/index.js +1 -0
  80. package/lib/esm/libs/openapi/util/call.js +8 -0
  81. package/lib/esm/libs/openapi/util/check-schema.js +62 -0
  82. package/lib/esm/libs/openapi/util/convert-schema.js +42 -0
  83. package/lib/esm/libs/openapi/util/flatten-open-api.js +19 -0
  84. package/lib/esm/libs/openapi/util/get-open-api-i18n-text.js +5 -0
  85. package/lib/esm/logger.js +2 -0
  86. package/lib/esm/runtime/resource-blocklet.js +2 -0
  87. package/lib/esm/runtime/runtime.js +136 -0
  88. package/lib/esm/types/assistant/index.js +9 -0
  89. package/lib/esm/types/assistant/mustache/ReadableMustache.js +63 -0
  90. package/lib/esm/types/assistant/mustache/directive.js +29 -0
  91. package/lib/esm/types/assistant/mustache/mustache.js +686 -0
  92. package/lib/esm/types/common/index.js +1 -0
  93. package/lib/esm/types/index.js +4 -0
  94. package/lib/esm/types/resource/index.js +26 -0
  95. package/lib/esm/types/resource/project.js +29 -0
  96. package/lib/esm/types/runtime/agent.js +1 -0
  97. package/lib/esm/types/runtime/error.js +14 -0
  98. package/lib/esm/types/runtime/index.js +20 -0
  99. package/lib/esm/types/runtime/runtime-resource-blocklet-state.js +1 -0
  100. package/lib/esm/types/runtime/schema.js +249 -0
  101. package/lib/esm/utils/cron-job.js +44 -0
  102. package/lib/esm/utils/extract-metadata-transform.js +54 -0
  103. package/lib/esm/utils/extract-metadata-transform.test.js +59 -0
  104. package/lib/esm/utils/fs.js +41 -0
  105. package/lib/esm/utils/get-blocklet-agent.js +344 -0
  106. package/lib/esm/utils/geti.js +30 -0
  107. package/lib/esm/utils/is-non-nullable.js +13 -0
  108. package/lib/esm/utils/render-message.js +20 -0
  109. package/lib/esm/utils/resolve-secret-inputs.js +46 -0
  110. package/lib/esm/utils/retry.js +16 -0
  111. package/lib/esm/utils/task-id.js +3 -0
  112. package/lib/esm/utils/tool-calls-transform.js +15 -0
  113. package/lib/types/assistant/generate-output.d.ts +29 -0
  114. package/lib/types/assistant/select-agent.d.ts +14 -0
  115. package/lib/types/assistant/type.d.ts +61 -0
  116. package/lib/types/common/aid.d.ts +18 -0
  117. package/lib/types/common/index.d.ts +7 -0
  118. package/lib/types/common/resource-manager.d.ts +88 -0
  119. package/lib/types/constants.d.ts +6 -0
  120. package/lib/types/executor/agent.d.ts +5 -0
  121. package/lib/types/executor/aigc.d.ts +9 -0
  122. package/lib/types/executor/api.d.ts +9 -0
  123. package/lib/types/executor/base.d.ts +209 -0
  124. package/lib/types/executor/blocklet.d.ts +9 -0
  125. package/lib/types/executor/call-agent.d.ts +12 -0
  126. package/lib/types/executor/decision.d.ts +20 -0
  127. package/lib/types/executor/image-blender.d.ts +9 -0
  128. package/lib/types/executor/index.d.ts +8 -0
  129. package/lib/types/executor/llm.d.ts +38 -0
  130. package/lib/types/executor/logic.d.ts +9 -0
  131. package/lib/types/index.d.ts +1 -0
  132. package/lib/types/libs/blocklet/vc.d.ts +17 -0
  133. package/lib/types/libs/openapi/request/index.d.ts +17 -0
  134. package/lib/types/libs/openapi/request/util.d.ts +40 -0
  135. package/lib/types/libs/openapi/types/index.d.ts +20 -0
  136. package/lib/types/libs/openapi/util/call.d.ts +2 -0
  137. package/lib/types/libs/openapi/util/check-schema.d.ts +3 -0
  138. package/lib/types/libs/openapi/util/convert-schema.d.ts +8 -0
  139. package/lib/types/libs/openapi/util/flatten-open-api.d.ts +3 -0
  140. package/lib/types/libs/openapi/util/get-open-api-i18n-text.d.ts +2 -0
  141. package/lib/types/logger.d.ts +2 -0
  142. package/lib/types/runtime/resource-blocklet.d.ts +2 -0
  143. package/lib/types/runtime/runtime.d.ts +20 -0
  144. package/lib/types/types/assistant/index.d.ts +405 -0
  145. package/lib/types/types/assistant/mustache/ReadableMustache.d.ts +2 -0
  146. package/lib/types/types/assistant/mustache/directive.d.ts +6 -0
  147. package/lib/types/types/assistant/mustache/mustache.d.ts +2 -0
  148. package/lib/types/types/common/index.d.ts +45 -0
  149. package/lib/types/types/index.d.ts +4 -0
  150. package/lib/types/types/resource/index.d.ts +17 -0
  151. package/lib/types/types/resource/project.d.ts +41 -0
  152. package/lib/types/types/runtime/agent.d.ts +33 -0
  153. package/lib/types/types/runtime/error.d.ts +10 -0
  154. package/lib/types/types/runtime/index.d.ts +116 -0
  155. package/lib/types/types/runtime/runtime-resource-blocklet-state.d.ts +5 -0
  156. package/lib/types/types/runtime/schema.d.ts +110 -0
  157. package/lib/types/utils/cron-job.d.ts +22 -0
  158. package/lib/types/utils/extract-metadata-transform.d.ts +16 -0
  159. package/lib/types/utils/extract-metadata-transform.test.d.ts +1 -0
  160. package/lib/types/utils/fs.d.ts +9 -0
  161. package/lib/types/utils/get-blocklet-agent.d.ts +219 -0
  162. package/lib/types/utils/geti.d.ts +1 -0
  163. package/lib/types/utils/is-non-nullable.d.ts +2 -0
  164. package/lib/types/utils/render-message.d.ts +6 -0
  165. package/lib/types/utils/resolve-secret-inputs.d.ts +11 -0
  166. package/lib/types/utils/retry.d.ts +1 -0
  167. package/lib/types/utils/task-id.d.ts +1 -0
  168. package/lib/types/utils/tool-calls-transform.d.ts +2 -0
  169. package/package.json +67 -0
  170. package/tsconfig.json +12 -0
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./assistant"), exports);
18
+ __exportStar(require("./runtime"), exports);
19
+ __exportStar(require("./common"), exports);
20
+ __exportStar(require("./resource"), exports);
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.ResourceTypes = void 0;
21
+ exports.validateResourceProject = validateResourceProject;
22
+ const joi_1 = __importDefault(require("joi"));
23
+ const project_1 = require("./project");
24
+ __exportStar(require("./project"), exports);
25
+ exports.ResourceTypes = [
26
+ 'application',
27
+ 'tool',
28
+ 'llm-adapter',
29
+ 'aigc-adapter',
30
+ 'knowledge',
31
+ 'template',
32
+ 'example',
33
+ ];
34
+ const resourceProjectSchema = joi_1.default.object({
35
+ project: project_1.projectSettingsSchema.required(),
36
+ agents: joi_1.default.array().items(joi_1.default.object()).required(),
37
+ config: joi_1.default.object().default({}),
38
+ cron: joi_1.default.object().default({}),
39
+ memory: joi_1.default.object().default({}),
40
+ }).options({ stripUnknown: true });
41
+ async function validateResourceProject(value) {
42
+ return resourceProjectSchema.validateAsync({
43
+ ...value,
44
+ agents: value.agents || value.assistants,
45
+ cron: value.cron || value.cronConfig,
46
+ });
47
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.projectSettingsSchema = void 0;
7
+ const joi_1 = __importDefault(require("joi"));
8
+ exports.projectSettingsSchema = joi_1.default.object({
9
+ id: joi_1.default.string().required(),
10
+ name: joi_1.default.string().empty(['', null]),
11
+ description: joi_1.default.string().empty(['', null]),
12
+ model: joi_1.default.string().empty(['', null]),
13
+ temperature: joi_1.default.number().empty(['', null]),
14
+ topP: joi_1.default.number().empty(['', null]),
15
+ presencePenalty: joi_1.default.number().empty(['', null]),
16
+ frequencyPenalty: joi_1.default.number().empty(['', null]),
17
+ maxTokens: joi_1.default.number().empty(['', null]),
18
+ createdAt: joi_1.default.alternatives(joi_1.default.string().isoDate(), joi_1.default.date().cast('string')).empty(['', null]),
19
+ updatedAt: joi_1.default.alternatives(joi_1.default.string().isoDate(), joi_1.default.date().cast('string')).empty(['', null]),
20
+ createdBy: joi_1.default.string().empty(['', null]),
21
+ updatedBy: joi_1.default.string().empty(['', null]),
22
+ appearance: joi_1.default.object({
23
+ primaryColor: joi_1.default.string().empty(['', null]),
24
+ typography: joi_1.default.object({
25
+ fontFamily: joi_1.default.string().empty(['', null]),
26
+ heading: joi_1.default.object({
27
+ fontFamily: joi_1.default.string().empty(['', null]),
28
+ }).empty(['', null]),
29
+ }).empty(['', null]),
30
+ }).empty(['', null]),
31
+ readme: joi_1.default.string().empty(['', null]).optional(),
32
+ executor: joi_1.default.object().empty(['', null]).optional(),
33
+ })
34
+ .rename('_id', 'id', { override: true, ignoreUndefined: true })
35
+ .options({ stripUnknown: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RuntimeError = exports.RuntimeErrorType = void 0;
4
+ var RuntimeErrorType;
5
+ (function (RuntimeErrorType) {
6
+ RuntimeErrorType["MissingSecretError"] = "MissingSecretError";
7
+ RuntimeErrorType["RequestExceededError"] = "RequestExceededError";
8
+ RuntimeErrorType["ProjectOwnerRequestExceededError"] = "ProjectOwnerRequestExceededError";
9
+ RuntimeErrorType["ProjectLimitExceededError"] = "ProjectLimitExceededError";
10
+ })(RuntimeErrorType || (exports.RuntimeErrorType = RuntimeErrorType = {}));
11
+ class RuntimeError extends Error {
12
+ type;
13
+ constructor(type, message) {
14
+ super(message);
15
+ this.type = type;
16
+ }
17
+ }
18
+ exports.RuntimeError = RuntimeError;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.AssistantResponseType = exports.ExecutionPhase = void 0;
18
+ __exportStar(require("./schema"), exports);
19
+ __exportStar(require("./error"), exports);
20
+ var ExecutionPhase;
21
+ (function (ExecutionPhase) {
22
+ ExecutionPhase["EXECUTE_BLOCK_START"] = "EXECUTE_BLOCK_START";
23
+ ExecutionPhase["EXECUTE_SELECT_STOP"] = "EXECUTE_SELECT_STOP";
24
+ ExecutionPhase["EXECUTE_ASSISTANT_START"] = "EXECUTE_ASSISTANT_START";
25
+ ExecutionPhase["EXECUTE_ASSISTANT_RUNNING"] = "EXECUTE_BLOCK_RUNNING";
26
+ ExecutionPhase["EXECUTE_ASSISTANT_END"] = "EXECUTE_ASSISTANT_END";
27
+ })(ExecutionPhase || (exports.ExecutionPhase = ExecutionPhase = {}));
28
+ var AssistantResponseType;
29
+ (function (AssistantResponseType) {
30
+ AssistantResponseType["ERROR"] = "ERROR";
31
+ AssistantResponseType["LOG"] = "LOG";
32
+ AssistantResponseType["INPUT"] = "INPUT";
33
+ AssistantResponseType["CHUNK"] = "CHUNK";
34
+ AssistantResponseType["USAGE"] = "USAGE";
35
+ AssistantResponseType["EXECUTE"] = "EXECUTE";
36
+ AssistantResponseType["INPUT_PARAMETER"] = "INPUT_PARAMETER";
37
+ })(AssistantResponseType || (exports.AssistantResponseType = AssistantResponseType = {}));
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RUNTIME_RESOURCE_BLOCKLET_STATE_GLOBAL_VARIABLE = void 0;
4
+ exports.RUNTIME_RESOURCE_BLOCKLET_STATE_GLOBAL_VARIABLE = '__AI_RUNTIME_RESOURCE_BLOCKLET_STATE__';
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RuntimeOutputVariable = exports.runtimeVariablesSchema = exports.variableBlockListForAgent = void 0;
7
+ exports.outputVariablesToJsonSchema = outputVariablesToJsonSchema;
8
+ exports.outputVariablesToJoiSchema = outputVariablesToJoiSchema;
9
+ exports.isRuntimeOutputVariable = isRuntimeOutputVariable;
10
+ exports.jsonSchemaToOpenAIJsonSchema = jsonSchemaToOpenAIJsonSchema;
11
+ const joi_1 = __importDefault(require("joi"));
12
+ const omitBy_1 = __importDefault(require("lodash/omitBy"));
13
+ const toLower_1 = __importDefault(require("lodash/toLower"));
14
+ exports.variableBlockListForAgent = {
15
+ prompt: {
16
+ // block: new Set(['$images']),
17
+ },
18
+ image: {
19
+ // allow: new Set(['$images']),
20
+ },
21
+ };
22
+ exports.runtimeVariablesSchema = {
23
+ $text: {
24
+ type: 'string',
25
+ description: 'Text Stream',
26
+ faker: 'lorem.paragraph',
27
+ },
28
+ $images: {
29
+ type: 'array',
30
+ description: 'Generated Images',
31
+ element: {
32
+ id: '',
33
+ type: 'object',
34
+ properties: [
35
+ {
36
+ id: '',
37
+ type: 'string',
38
+ name: 'url',
39
+ description: 'Image Url',
40
+ required: true,
41
+ faker: 'image.url',
42
+ },
43
+ ],
44
+ },
45
+ },
46
+ '$suggested.questions': {
47
+ type: 'array',
48
+ description: 'Generate 3 questions for users to ask you based on answers and context',
49
+ element: {
50
+ id: '',
51
+ type: 'object',
52
+ properties: [
53
+ {
54
+ id: '',
55
+ type: 'string',
56
+ name: 'question',
57
+ required: true,
58
+ faker: 'lorem.sentence',
59
+ },
60
+ ],
61
+ },
62
+ required: true,
63
+ },
64
+ '$reference.links': {
65
+ type: 'array',
66
+ description: 'List all referenced links in the generated text',
67
+ element: {
68
+ id: '',
69
+ type: 'object',
70
+ properties: [
71
+ {
72
+ id: '',
73
+ type: 'string',
74
+ name: 'title',
75
+ required: true,
76
+ faker: 'lorem.sentence',
77
+ },
78
+ {
79
+ id: '',
80
+ type: 'string',
81
+ name: 'url',
82
+ required: true,
83
+ faker: 'internet.url',
84
+ },
85
+ ],
86
+ },
87
+ required: true,
88
+ },
89
+ };
90
+ function outputVariablesToJsonSchema(assistant, { variables, includeRuntimeOutputVariables, includeFaker, }) {
91
+ const variableToSchema = (variable) => {
92
+ if (variable.from?.type === 'input')
93
+ return undefined;
94
+ if (!includeRuntimeOutputVariables && ignoreJsonSchemaOutputs.has(variable.name))
95
+ return undefined;
96
+ if (variable.name && isRuntimeOutputVariable(variable.name)) {
97
+ const runtimeVariable = exports.runtimeVariablesSchema[variable.name];
98
+ if (!runtimeVariable)
99
+ return undefined;
100
+ return variableToSchema({
101
+ ...runtimeVariable,
102
+ description: [runtimeVariable.description, variable.description].filter((i) => !!i).join('\n'),
103
+ });
104
+ }
105
+ if (variable.variable) {
106
+ const { key, scope } = variable.variable;
107
+ const v = variables.find((i) => (0, toLower_1.default)(i.key) === (0, toLower_1.default)(key) && i.scope === scope);
108
+ if (!v?.type)
109
+ throw new Error(`Variable ${key} not found from ${scope}`);
110
+ return variableToSchema(v.type);
111
+ }
112
+ return (0, omitBy_1.default)({
113
+ type: variable.type || 'string',
114
+ description: variable.description,
115
+ properties: variable.type === 'object' && variable.properties
116
+ ? Object.fromEntries(variable.properties
117
+ .map((property) => [property.name, variableToSchema(property)])
118
+ .filter((i) => i[0] && i[1]))
119
+ : undefined,
120
+ items: variable.type === 'array' && variable.element ? variableToSchema(variable.element) : undefined,
121
+ additionalProperties: variable.type === 'object' ? false : undefined,
122
+ required: variable.type === 'object' && variable.properties?.length
123
+ ? variable.properties
124
+ .filter((i) => i.name &&
125
+ (i.required ||
126
+ (runtimeOutputVariableSet.has(i.name) &&
127
+ exports.runtimeVariablesSchema[i.name] &&
128
+ !ignoreJsonSchemaOutputs.has(i.name))))
129
+ .map((i) => i.name)
130
+ : undefined,
131
+ faker: includeFaker ? variable.faker : undefined,
132
+ }, (v) => v === undefined);
133
+ };
134
+ const outputVariables = (assistant.outputVariables ?? []).filter((i) => !i.hidden && i.from?.type !== 'callAgent' && !i.valueTemplate?.trim());
135
+ return variableToSchema({ type: 'object', properties: outputVariables });
136
+ }
137
+ function outputVariablesToJoiSchema(assistant, { partial, variables }) {
138
+ const variableToSchema = (variable) => {
139
+ let schema;
140
+ if (variable.from?.type === 'input') {
141
+ const fromId = variable.from.id;
142
+ const input = assistant.parameters?.find((i) => i.id === fromId && !i.hidden);
143
+ if (input) {
144
+ return joi_1.default.any();
145
+ }
146
+ return undefined;
147
+ }
148
+ if (variable.from?.type === 'output') {
149
+ return joi_1.default.any();
150
+ }
151
+ if (variable.name && isRuntimeOutputVariable(variable.name)) {
152
+ if (variable.name === RuntimeOutputVariable.llmResponseStream) {
153
+ schema = joi_1.default.any();
154
+ }
155
+ else {
156
+ const runtimeVariable = exports.runtimeVariablesSchema[variable.name];
157
+ if (!runtimeVariable)
158
+ return undefined;
159
+ schema = variableToSchema({ ...runtimeVariable });
160
+ if (schema) {
161
+ schema = joi_1.default.alternatives().try(schema, joi_1.default.any().empty(joi_1.default.any()));
162
+ }
163
+ }
164
+ return schema;
165
+ }
166
+ if (variable.variable) {
167
+ const { key, scope } = variable.variable;
168
+ const v = variables.find((i) => (0, toLower_1.default)(i.key) === (0, toLower_1.default)(key) && i.scope === scope);
169
+ if (!v?.type)
170
+ return undefined;
171
+ schema = variableToSchema(v.type);
172
+ }
173
+ else if (!variable.type || variable.type === 'string') {
174
+ schema = joi_1.default.string().empty(['', null]);
175
+ }
176
+ else if (variable.type === 'number') {
177
+ schema = joi_1.default.number().empty([null, '']);
178
+ }
179
+ else if (variable.type === 'boolean') {
180
+ schema = joi_1.default.boolean().empty([null, '']);
181
+ }
182
+ else if (variable.type === 'object') {
183
+ if (variable.properties?.length) {
184
+ schema = joi_1.default.object(Object.fromEntries((variable.properties ?? [])
185
+ .map((property) => [property.name, variableToSchema(property)])
186
+ .filter((i) => i[0] && i[1])))
187
+ .empty([null, ''])
188
+ .options({ stripUnknown: true });
189
+ }
190
+ else {
191
+ schema = joi_1.default.any().empty(['', null]);
192
+ }
193
+ }
194
+ else if (variable.type === 'array') {
195
+ schema = joi_1.default.array()
196
+ .empty([null, ''])
197
+ .items((variable.element && variableToSchema(variable.element)) || joi_1.default.string().empty([null, '']));
198
+ }
199
+ if (!schema)
200
+ return undefined;
201
+ if ('defaultValue' in variable) {
202
+ schema = schema.default(variable.defaultValue);
203
+ }
204
+ if (variable.required) {
205
+ schema = schema.required();
206
+ }
207
+ return schema;
208
+ };
209
+ const outputVariables = (assistant.outputVariables ?? []).filter((i) => !i.hidden && i.from?.type !== 'callAgent' && !i.valueTemplate);
210
+ return variableToSchema({
211
+ type: 'object',
212
+ properties: partial ? outputVariables.map((i) => ({ ...i, required: false })) : outputVariables,
213
+ });
214
+ }
215
+ var RuntimeOutputVariable;
216
+ (function (RuntimeOutputVariable) {
217
+ RuntimeOutputVariable["llmResponseStream"] = "$llmResponseStream";
218
+ RuntimeOutputVariable["text"] = "$text";
219
+ RuntimeOutputVariable["images"] = "$images";
220
+ RuntimeOutputVariable["suggestedQuestions"] = "$suggested.questions";
221
+ RuntimeOutputVariable["referenceLinks"] = "$reference.links";
222
+ RuntimeOutputVariable["appearancePage"] = "$appearance.page";
223
+ RuntimeOutputVariable["appearanceInput"] = "$appearance.input";
224
+ RuntimeOutputVariable["appearanceOutput"] = "$appearance.output";
225
+ RuntimeOutputVariable["children"] = "$children";
226
+ RuntimeOutputVariable["share"] = "$share";
227
+ RuntimeOutputVariable["openingQuestions"] = "$openingQuestions";
228
+ RuntimeOutputVariable["openingMessage"] = "$openingMessage";
229
+ RuntimeOutputVariable["profile"] = "$profile";
230
+ })(RuntimeOutputVariable || (exports.RuntimeOutputVariable = RuntimeOutputVariable = {}));
231
+ const runtimeOutputVariableSet = new Set(Object.values(RuntimeOutputVariable));
232
+ const ignoreJsonSchemaOutputs = new Set([
233
+ RuntimeOutputVariable.text,
234
+ RuntimeOutputVariable.images,
235
+ ]);
236
+ function isRuntimeOutputVariable(variable) {
237
+ return Object.values(RuntimeOutputVariable).includes(variable);
238
+ }
239
+ function jsonSchemaToOpenAIJsonSchema(schema) {
240
+ if (schema?.type === 'object') {
241
+ const { required, properties } = schema;
242
+ return {
243
+ ...schema,
244
+ properties: Object.fromEntries(Object.entries(properties).map(([key, value]) => {
245
+ const valueSchema = jsonSchemaToOpenAIJsonSchema(value);
246
+ // NOTE: All fields must be required https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
247
+ return [key, required?.includes(key) ? valueSchema : { anyOf: [valueSchema, { type: ['null'] }] }];
248
+ })),
249
+ required: Object.keys(properties),
250
+ };
251
+ }
252
+ if (schema?.type === 'array') {
253
+ return {
254
+ ...schema,
255
+ items: jsonSchemaToOpenAIJsonSchema(schema.items),
256
+ };
257
+ }
258
+ return schema;
259
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CronJobManager = exports.CronJobDefaultGroup = void 0;
4
+ const cron_1 = require("cron");
5
+ exports.CronJobDefaultGroup = 'default';
6
+ class CronJobManager {
7
+ groups = {};
8
+ resetJobs(jobs, { groupId = exports.CronJobDefaultGroup } = {}) {
9
+ this.destroyGroup({ groupId });
10
+ for (const job of jobs) {
11
+ this.resetJob(job, { groupId });
12
+ }
13
+ }
14
+ resetJob(job, { groupId = exports.CronJobDefaultGroup } = {}) {
15
+ if (!job.cronTime)
16
+ throw new Error('cronTime is required');
17
+ this.groups[groupId] ??= { jobs: {} };
18
+ const group = this.groups[groupId];
19
+ this.stopJob(job.id, { groupId });
20
+ const cronJob = cron_1.CronJob.from({ cronTime: job.cronTime, onTick: job.onTick, utcOffset: 0, start: true });
21
+ group.jobs[job.id] = { cronJob, job };
22
+ }
23
+ stopJob(jobId, { groupId = exports.CronJobDefaultGroup } = {}) {
24
+ const group = this.groups[groupId];
25
+ if (!group)
26
+ return;
27
+ const item = group.jobs[jobId];
28
+ if (!item)
29
+ return;
30
+ item.cronJob.stop();
31
+ delete group.jobs[jobId];
32
+ }
33
+ destroyGroup({ groupId }) {
34
+ const group = this.groups[groupId];
35
+ if (!group)
36
+ return;
37
+ for (const item of Object.values(group.jobs)) {
38
+ this.stopJob(item.job.id, { groupId });
39
+ }
40
+ delete this.groups[groupId];
41
+ }
42
+ destroy() {
43
+ for (const groupId of Object.keys(this.groups)) {
44
+ this.destroyGroup({ groupId });
45
+ }
46
+ }
47
+ }
48
+ exports.CronJobManager = CronJobManager;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExtractMetadataTransform = void 0;
4
+ const web_1 = require("stream/web");
5
+ class ExtractMetadataTransform extends web_1.TransformStream {
6
+ buffer = '';
7
+ cursor = 0;
8
+ state = 'none';
9
+ constructor({ start, end }) {
10
+ super({
11
+ transform: async (chunk, controller) => {
12
+ if (!start || !end) {
13
+ controller.enqueue({ type: 'text', text: chunk });
14
+ return;
15
+ }
16
+ this.buffer += chunk;
17
+ for (;;) {
18
+ if (this.state === 'none') {
19
+ const found = findMatchIndex(this.buffer, this.cursor, start);
20
+ if (found.start > this.cursor) {
21
+ const text = this.buffer.slice(this.cursor, found.start);
22
+ this.cursor = found.start;
23
+ controller.enqueue({ type: 'text', text });
24
+ }
25
+ if (found.end) {
26
+ this.state = 'start';
27
+ this.cursor = found.end;
28
+ }
29
+ }
30
+ if (this.state === 'start') {
31
+ const found = findMatchIndex(this.buffer, this.cursor, end);
32
+ if (found.end) {
33
+ const match = this.buffer.slice(this.cursor, found.start);
34
+ controller.enqueue({ type: 'match', text: match });
35
+ this.state = 'none';
36
+ this.cursor = found.end;
37
+ continue;
38
+ }
39
+ }
40
+ break;
41
+ }
42
+ },
43
+ });
44
+ }
45
+ }
46
+ exports.ExtractMetadataTransform = ExtractMetadataTransform;
47
+ function findMatchIndex(str, position, match) {
48
+ const i = str.indexOf(match, position);
49
+ if (i >= 0)
50
+ return { start: i, end: i + match.length };
51
+ for (let i = match.length - 1; i > 0; i--) {
52
+ const m = match.slice(0, i);
53
+ if (str.endsWith(m)) {
54
+ return { start: str.length - m.length };
55
+ }
56
+ }
57
+ return { start: str.length };
58
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const web_1 = require("stream/web");
4
+ const globals_1 = require("@jest/globals");
5
+ const extract_metadata_transform_1 = require("./extract-metadata-transform");
6
+ (0, globals_1.describe)('ExtractMetadataTransform', () => {
7
+ async function run(str, start, end, step = 1) {
8
+ const s = new web_1.ReadableStream({
9
+ start(controller) {
10
+ let t = '';
11
+ for (const i of str) {
12
+ t += i;
13
+ if (t.length === step) {
14
+ controller.enqueue(t);
15
+ t = '';
16
+ }
17
+ }
18
+ controller.enqueue(t);
19
+ controller.close();
20
+ },
21
+ }).pipeThrough(new extract_metadata_transform_1.ExtractMetadataTransform({ start, end }));
22
+ let text = '';
23
+ const matched = [];
24
+ for await (const i of s) {
25
+ if (i.type === 'text') {
26
+ text += i.text;
27
+ }
28
+ else if (i.type === 'match') {
29
+ matched.push(i.text);
30
+ }
31
+ }
32
+ return { text, matched };
33
+ }
34
+ const cases = [
35
+ {
36
+ input: 'hello<metadata>{"name":"foo","age":120}</metadata>world how<metadata>{"name":"bar","age":130}</metadata> to',
37
+ start: '<metadata>',
38
+ end: '</metadata>',
39
+ expected: { text: 'helloworld how to', matched: ['{"name":"foo","age":120}', '{"name":"bar","age":130}'] },
40
+ },
41
+ {
42
+ input: '```metadata{"name":"foo","age":120}```',
43
+ start: '```metadata',
44
+ end: '```',
45
+ expected: { text: '', matched: ['{"name":"foo","age":120}'] },
46
+ },
47
+ {
48
+ input: '```metadata{"name":"foo","age":120}``````metadata{"name":"bar","age":130}```',
49
+ start: '```metadata',
50
+ end: '```',
51
+ expected: { text: '', matched: ['{"name":"foo","age":120}', '{"name":"bar","age":130}'] },
52
+ },
53
+ ];
54
+ (0, globals_1.test)('extract metadata transform', async () => {
55
+ for (const c of cases) {
56
+ for (let i = 1; i < c.input.length; i++) {
57
+ (0, globals_1.expect)([await run(c.input, c.start, c.end, i), i]).toEqual([c.expected, i]);
58
+ }
59
+ }
60
+ });
61
+ });
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.copyFile = copyFile;
7
+ exports.copyDirectory = copyDirectory;
8
+ exports.copyRecursive = copyRecursive;
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
11
+ async function copyFile(src, dest, { skip } = {}) {
12
+ if (skip?.(src))
13
+ return;
14
+ await new Promise((resolve, reject) => {
15
+ const readStream = fs_1.default.createReadStream(src);
16
+ const writeStream = fs_1.default.createWriteStream(dest);
17
+ readStream.on('error', reject);
18
+ writeStream.on('error', reject);
19
+ writeStream.on('finish', resolve);
20
+ readStream.pipe(writeStream);
21
+ });
22
+ }
23
+ async function copyDirectory(src, dest, { skip } = {}) {
24
+ if (skip?.(src))
25
+ return;
26
+ await fs_1.default.promises.mkdir(dest, { recursive: true });
27
+ const entries = await fs_1.default.promises.readdir(src, { withFileTypes: true });
28
+ for (const entry of entries) {
29
+ const srcPath = path_1.default.join(src, entry.name);
30
+ const destPath = path_1.default.join(dest, entry.name);
31
+ if (entry.isDirectory()) {
32
+ await copyDirectory(srcPath, destPath, { skip });
33
+ }
34
+ else {
35
+ await copyFile(srcPath, destPath, { skip });
36
+ }
37
+ }
38
+ }
39
+ async function copyRecursive(src, dest, { skip } = {}) {
40
+ if (skip?.(src))
41
+ return;
42
+ const srcStats = await fs_1.default.promises.stat(src);
43
+ if (srcStats.isDirectory()) {
44
+ await copyDirectory(src, dest, { skip });
45
+ }
46
+ else {
47
+ await copyFile(src, dest, { skip });
48
+ }
49
+ }