@stackfactor/agent-utils 1.13.4 → 1.32.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.
- package/README.md +667 -23
- package/dist/cjs/agentProto.d.ts +13 -3
- package/dist/cjs/agentProto.d.ts.map +1 -1
- package/dist/cjs/agentProto.js +25 -4
- package/dist/cjs/backoff.d.ts +17 -0
- package/dist/cjs/backoff.d.ts.map +1 -0
- package/dist/cjs/backoff.js +26 -0
- package/dist/cjs/cancellation.d.ts +22 -0
- package/dist/cjs/cancellation.d.ts.map +1 -0
- package/dist/cjs/cancellation.js +36 -0
- package/dist/cjs/cli.d.ts +3 -0
- package/dist/cjs/cli.d.ts.map +1 -0
- package/dist/cjs/cli.js +85 -0
- package/dist/cjs/client.d.ts +11 -0
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +27 -0
- package/dist/cjs/const.d.ts +23 -0
- package/dist/cjs/const.d.ts.map +1 -1
- package/dist/cjs/const.js +23 -0
- package/dist/cjs/daemon.d.ts +94 -0
- package/dist/cjs/daemon.d.ts.map +1 -0
- package/dist/cjs/daemon.js +507 -0
- package/dist/cjs/daemonConfig.d.ts +45 -0
- package/dist/cjs/daemonConfig.d.ts.map +1 -0
- package/dist/cjs/daemonConfig.js +148 -0
- package/dist/cjs/daemonEnv.d.ts +10 -0
- package/dist/cjs/daemonEnv.d.ts.map +1 -0
- package/dist/cjs/daemonEnv.js +12 -0
- package/dist/cjs/durable/clientTool.d.ts +39 -0
- package/dist/cjs/durable/clientTool.d.ts.map +1 -0
- package/dist/cjs/durable/clientTool.js +90 -0
- package/dist/cjs/durable/index.d.ts +9 -0
- package/dist/cjs/durable/index.d.ts.map +1 -0
- package/dist/cjs/durable/index.js +16 -0
- package/dist/cjs/durable/journal.d.ts +218 -0
- package/dist/cjs/durable/journal.d.ts.map +1 -0
- package/dist/cjs/durable/journal.js +392 -0
- package/dist/cjs/durable/pause.d.ts +84 -0
- package/dist/cjs/durable/pause.d.ts.map +1 -0
- package/dist/cjs/durable/pause.js +241 -0
- package/dist/cjs/durable/resume.d.ts +27 -0
- package/dist/cjs/durable/resume.d.ts.map +1 -0
- package/dist/cjs/durable/resume.js +80 -0
- package/dist/cjs/durable/snapshotSaver.d.ts +38 -0
- package/dist/cjs/durable/snapshotSaver.d.ts.map +1 -0
- package/dist/cjs/durable/snapshotSaver.js +81 -0
- package/dist/cjs/durable/step.d.ts +28 -0
- package/dist/cjs/durable/step.d.ts.map +1 -0
- package/dist/cjs/durable/step.js +43 -0
- package/dist/cjs/index.d.ts +30 -7
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +28 -1
- package/dist/cjs/infrastructure.d.ts +50 -0
- package/dist/cjs/infrastructure.d.ts.map +1 -0
- package/dist/cjs/infrastructure.js +163 -0
- package/dist/cjs/langChain.d.ts +15 -3
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +986 -180
- package/dist/cjs/learningActivityMedia.d.ts +196 -0
- package/dist/cjs/learningActivityMedia.d.ts.map +1 -0
- package/dist/cjs/learningActivityMedia.js +861 -0
- package/dist/cjs/learningContentPlan.d.ts +105 -0
- package/dist/cjs/learningContentPlan.d.ts.map +1 -0
- package/dist/cjs/learningContentPlan.js +305 -0
- package/dist/cjs/learningContentThread.d.ts +184 -0
- package/dist/cjs/learningContentThread.d.ts.map +1 -0
- package/dist/cjs/learningContentThread.js +407 -0
- package/dist/cjs/logger.d.ts +1 -0
- package/dist/cjs/logger.d.ts.map +1 -1
- package/dist/cjs/logger.js +17 -3
- package/dist/cjs/modelRates.d.ts +40 -0
- package/dist/cjs/modelRates.d.ts.map +1 -0
- package/dist/cjs/modelRates.js +76 -0
- package/dist/cjs/referenceContext.d.ts +4 -145
- package/dist/cjs/referenceContext.d.ts.map +1 -1
- package/dist/cjs/referenceContext.js +16 -251
- package/dist/cjs/retry.d.ts +64 -0
- package/dist/cjs/retry.d.ts.map +1 -0
- package/dist/cjs/retry.js +138 -0
- package/dist/cjs/runtimeContext.d.ts +8 -1
- package/dist/cjs/runtimeContext.d.ts.map +1 -1
- package/dist/cjs/runtimeContext.js +3 -1
- package/dist/cjs/serve.d.ts +3 -2
- package/dist/cjs/serve.d.ts.map +1 -1
- package/dist/cjs/serve.js +32 -2
- package/dist/cjs/sourceStore.d.ts +378 -0
- package/dist/cjs/sourceStore.d.ts.map +1 -0
- package/dist/cjs/sourceStore.js +872 -0
- package/dist/cjs/stackfactorKnowledge.d.ts +91 -7
- package/dist/cjs/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/cjs/stackfactorKnowledge.js +108 -44
- package/dist/cjs/tavily.d.ts +108 -4
- package/dist/cjs/tavily.d.ts.map +1 -1
- package/dist/cjs/tavily.js +242 -21
- package/dist/cjs/usage.d.ts.map +1 -1
- package/dist/cjs/usage.js +9 -15
- package/dist/cjs/version.d.ts +3 -0
- package/dist/cjs/version.d.ts.map +1 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/workflow/bookkeeping.d.ts +78 -0
- package/dist/cjs/workflow/bookkeeping.d.ts.map +1 -0
- package/dist/cjs/workflow/bookkeeping.js +75 -0
- package/dist/cjs/workflow/index.d.ts +13 -0
- package/dist/cjs/workflow/index.d.ts.map +1 -0
- package/dist/cjs/workflow/index.js +51 -0
- package/dist/cjs/workflow/limits.d.ts +52 -0
- package/dist/cjs/workflow/limits.d.ts.map +1 -0
- package/dist/cjs/workflow/limits.js +62 -0
- package/dist/cjs/workflow/platformTransport.d.ts +39 -0
- package/dist/cjs/workflow/platformTransport.d.ts.map +1 -0
- package/dist/cjs/workflow/platformTransport.js +115 -0
- package/dist/cjs/workflow/retrying.d.ts +44 -0
- package/dist/cjs/workflow/retrying.d.ts.map +1 -0
- package/dist/cjs/workflow/retrying.js +82 -0
- package/dist/cjs/workflow/run.d.ts +195 -0
- package/dist/cjs/workflow/run.d.ts.map +1 -0
- package/dist/cjs/workflow/run.js +858 -0
- package/dist/cjs/workflow/testing/index.d.ts +3 -0
- package/dist/cjs/workflow/testing/index.d.ts.map +1 -0
- package/dist/cjs/workflow/testing/index.js +6 -0
- package/dist/cjs/workflow/testing/memoryTransport.d.ts +203 -0
- package/dist/cjs/workflow/testing/memoryTransport.d.ts.map +1 -0
- package/dist/cjs/workflow/testing/memoryTransport.js +1171 -0
- package/dist/cjs/workflow/transport.d.ts +219 -0
- package/dist/cjs/workflow/transport.d.ts.map +1 -0
- package/dist/cjs/workflow/transport.js +19 -0
- package/dist/esm/agentProto.d.ts +13 -3
- package/dist/esm/agentProto.d.ts.map +1 -1
- package/dist/esm/agentProto.js +23 -3
- package/dist/esm/backoff.d.ts +17 -0
- package/dist/esm/backoff.d.ts.map +1 -0
- package/dist/esm/backoff.js +22 -0
- package/dist/esm/cancellation.d.ts +22 -0
- package/dist/esm/cancellation.d.ts.map +1 -0
- package/dist/esm/cancellation.js +27 -0
- package/dist/esm/cli.d.ts +3 -0
- package/dist/esm/cli.d.ts.map +1 -0
- package/dist/esm/cli.js +80 -0
- package/dist/esm/client.d.ts +11 -0
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +25 -1
- package/dist/esm/const.d.ts +23 -0
- package/dist/esm/const.d.ts.map +1 -1
- package/dist/esm/const.js +23 -0
- package/dist/esm/daemon.d.ts +94 -0
- package/dist/esm/daemon.d.ts.map +1 -0
- package/dist/esm/daemon.js +499 -0
- package/dist/esm/daemonConfig.d.ts +45 -0
- package/dist/esm/daemonConfig.d.ts.map +1 -0
- package/dist/esm/daemonConfig.js +142 -0
- package/dist/esm/daemonEnv.d.ts +10 -0
- package/dist/esm/daemonEnv.d.ts.map +1 -0
- package/dist/esm/daemonEnv.js +9 -0
- package/dist/esm/durable/clientTool.d.ts +39 -0
- package/dist/esm/durable/clientTool.d.ts.map +1 -0
- package/dist/esm/durable/clientTool.js +82 -0
- package/dist/esm/durable/index.d.ts +9 -0
- package/dist/esm/durable/index.d.ts.map +1 -0
- package/dist/esm/durable/index.js +8 -0
- package/dist/esm/durable/journal.d.ts +218 -0
- package/dist/esm/durable/journal.d.ts.map +1 -0
- package/dist/esm/durable/journal.js +374 -0
- package/dist/esm/durable/pause.d.ts +84 -0
- package/dist/esm/durable/pause.d.ts.map +1 -0
- package/dist/esm/durable/pause.js +226 -0
- package/dist/esm/durable/resume.d.ts +27 -0
- package/dist/esm/durable/resume.d.ts.map +1 -0
- package/dist/esm/durable/resume.js +72 -0
- package/dist/esm/durable/snapshotSaver.d.ts +38 -0
- package/dist/esm/durable/snapshotSaver.d.ts.map +1 -0
- package/dist/esm/durable/snapshotSaver.js +77 -0
- package/dist/esm/durable/step.d.ts +28 -0
- package/dist/esm/durable/step.d.ts.map +1 -0
- package/dist/esm/durable/step.js +36 -0
- package/dist/esm/index.d.ts +30 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +20 -4
- package/dist/esm/infrastructure.d.ts +50 -0
- package/dist/esm/infrastructure.d.ts.map +1 -0
- package/dist/esm/infrastructure.js +156 -0
- package/dist/esm/langChain.d.ts +15 -3
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +984 -179
- package/dist/esm/learningActivityMedia.d.ts +196 -0
- package/dist/esm/learningActivityMedia.d.ts.map +1 -0
- package/dist/esm/learningActivityMedia.js +807 -0
- package/dist/esm/learningContentPlan.d.ts +105 -0
- package/dist/esm/learningContentPlan.d.ts.map +1 -0
- package/dist/esm/learningContentPlan.js +299 -0
- package/dist/esm/learningContentThread.d.ts +184 -0
- package/dist/esm/learningContentThread.d.ts.map +1 -0
- package/dist/esm/learningContentThread.js +364 -0
- package/dist/esm/logger.d.ts +1 -0
- package/dist/esm/logger.d.ts.map +1 -1
- package/dist/esm/logger.js +17 -3
- package/dist/esm/modelRates.d.ts +40 -0
- package/dist/esm/modelRates.d.ts.map +1 -0
- package/dist/esm/modelRates.js +68 -0
- package/dist/esm/referenceContext.d.ts +4 -145
- package/dist/esm/referenceContext.d.ts.map +1 -1
- package/dist/esm/referenceContext.js +16 -243
- package/dist/esm/retry.d.ts +64 -0
- package/dist/esm/retry.d.ts.map +1 -0
- package/dist/esm/retry.js +127 -0
- package/dist/esm/runtimeContext.d.ts +8 -1
- package/dist/esm/runtimeContext.d.ts.map +1 -1
- package/dist/esm/runtimeContext.js +3 -1
- package/dist/esm/serve.d.ts +3 -2
- package/dist/esm/serve.d.ts.map +1 -1
- package/dist/esm/serve.js +33 -3
- package/dist/esm/sourceStore.d.ts +378 -0
- package/dist/esm/sourceStore.d.ts.map +1 -0
- package/dist/esm/sourceStore.js +855 -0
- package/dist/esm/stackfactorKnowledge.d.ts +91 -7
- package/dist/esm/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/esm/stackfactorKnowledge.js +101 -41
- package/dist/esm/tavily.d.ts +108 -4
- package/dist/esm/tavily.d.ts.map +1 -1
- package/dist/esm/tavily.js +237 -20
- package/dist/esm/usage.d.ts.map +1 -1
- package/dist/esm/usage.js +9 -15
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/workflow/bookkeeping.d.ts +78 -0
- package/dist/esm/workflow/bookkeeping.d.ts.map +1 -0
- package/dist/esm/workflow/bookkeeping.js +67 -0
- package/dist/esm/workflow/index.d.ts +13 -0
- package/dist/esm/workflow/index.d.ts.map +1 -0
- package/dist/esm/workflow/index.js +10 -0
- package/dist/esm/workflow/limits.d.ts +52 -0
- package/dist/esm/workflow/limits.d.ts.map +1 -0
- package/dist/esm/workflow/limits.js +59 -0
- package/dist/esm/workflow/platformTransport.d.ts +39 -0
- package/dist/esm/workflow/platformTransport.d.ts.map +1 -0
- package/dist/esm/workflow/platformTransport.js +110 -0
- package/dist/esm/workflow/retrying.d.ts +44 -0
- package/dist/esm/workflow/retrying.d.ts.map +1 -0
- package/dist/esm/workflow/retrying.js +73 -0
- package/dist/esm/workflow/run.d.ts +195 -0
- package/dist/esm/workflow/run.d.ts.map +1 -0
- package/dist/esm/workflow/run.js +850 -0
- package/dist/esm/workflow/testing/index.d.ts +3 -0
- package/dist/esm/workflow/testing/index.d.ts.map +1 -0
- package/dist/esm/workflow/testing/index.js +2 -0
- package/dist/esm/workflow/testing/memoryTransport.d.ts +203 -0
- package/dist/esm/workflow/testing/memoryTransport.d.ts.map +1 -0
- package/dist/esm/workflow/testing/memoryTransport.js +1167 -0
- package/dist/esm/workflow/transport.d.ts +219 -0
- package/dist/esm/workflow/transport.d.ts.map +1 -0
- package/dist/esm/workflow/transport.js +16 -0
- package/package.json +8 -7
|
@@ -0,0 +1,51 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.testing = exports.LIMITS = exports.retrying = exports.platformTransport = exports.messageAdapter = exports.run = void 0;
|
|
37
|
+
// The `workflow` module (runner spec): `workflow.run`, its transports
|
|
38
|
+
// (`platformTransport`, `retrying`), `workflow.LIMITS` and the in-memory
|
|
39
|
+
// transport, `workflow.testing.memoryTransport()`, with the wire types an
|
|
40
|
+
// agent's turns are written against.
|
|
41
|
+
const testing = __importStar(require("./testing/index.js"));
|
|
42
|
+
exports.testing = testing;
|
|
43
|
+
var run_js_1 = require("./run.js");
|
|
44
|
+
Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_js_1.run; } });
|
|
45
|
+
Object.defineProperty(exports, "messageAdapter", { enumerable: true, get: function () { return run_js_1.messageAdapter; } });
|
|
46
|
+
var platformTransport_js_1 = require("./platformTransport.js");
|
|
47
|
+
Object.defineProperty(exports, "platformTransport", { enumerable: true, get: function () { return platformTransport_js_1.platformTransport; } });
|
|
48
|
+
var retrying_js_1 = require("./retrying.js");
|
|
49
|
+
Object.defineProperty(exports, "retrying", { enumerable: true, get: function () { return retrying_js_1.retrying; } });
|
|
50
|
+
var limits_js_1 = require("./limits.js");
|
|
51
|
+
Object.defineProperty(exports, "LIMITS", { enumerable: true, get: function () { return limits_js_1.LIMITS; } });
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The limits the platform enforces on an agent workflow (runner spec §2):
|
|
3
|
+
* backend-core's `INTEGRATIONS.WORKFLOW` values, every number and pattern of
|
|
4
|
+
* it, plus the lease's default length (`AGENT_POOL.TASK_LEASE_DEFAULT_SECONDS`,
|
|
5
|
+
* which agent pools §13 put in place of `SERVICE_LEASE_SECONDS` and
|
|
6
|
+
* `JOB_LEASE_DEFAULT_SECONDS`). The in-memory transport enforces them, and a
|
|
7
|
+
* turn can plan around them (declare fewer waits, keep `state` small).
|
|
8
|
+
*
|
|
9
|
+
* Core's contract test asserts its constants equal these (runner spec §6,
|
|
10
|
+
* change 3), so the two cannot drift without a failing test. Frozen, so no
|
|
11
|
+
* agent can loosen a limit for every other caller in the process.
|
|
12
|
+
*/
|
|
13
|
+
export declare const LIMITS: Readonly<{
|
|
14
|
+
STATE_MAX_BYTES: number;
|
|
15
|
+
INBOX_MAX: 200;
|
|
16
|
+
INBOX_EVENT_DATA_MAX_BYTES: number;
|
|
17
|
+
SIGNAL_INPUT_MAX_BYTES: number;
|
|
18
|
+
OPEN_WAITS_MAX: 50;
|
|
19
|
+
OPEN_INPUT_WAITS_MAX: 16;
|
|
20
|
+
OPEN_WAITS_MAX_BYTES: number;
|
|
21
|
+
SETTLED_WAITS_KEPT: 100;
|
|
22
|
+
CHECKPOINT_MAX_BYTES: number;
|
|
23
|
+
QUEUED_RUN_RETRY_SECONDS: 60;
|
|
24
|
+
ENDED_RETENTION_DAYS: 30;
|
|
25
|
+
MAX_TURNS: 1000;
|
|
26
|
+
MAX_TURNS_PER_HOUR: 60;
|
|
27
|
+
MIN_TIMER_SECONDS: 60;
|
|
28
|
+
INPUT_MAX_ATTEMPTS: 3;
|
|
29
|
+
WORKFLOW_SWEEP_PER_TENANT: 500;
|
|
30
|
+
KEY_PATTERN: RegExp;
|
|
31
|
+
WAIT_TAG_PATTERN: RegExp;
|
|
32
|
+
WAIT_FIELD_MAX_CHARS: 128;
|
|
33
|
+
CODE_MAX_CHARS: 256;
|
|
34
|
+
CONTACTS_MAX: 20;
|
|
35
|
+
ADDRESSEES_MAX: 20;
|
|
36
|
+
MAX_QUESTIONS_PER_WAIT: 20;
|
|
37
|
+
QUESTIONS_MAX_BYTES: number;
|
|
38
|
+
ANSWERS_MAX_BYTES: number;
|
|
39
|
+
TITLE_MAX_CHARS: 200;
|
|
40
|
+
QUESTION_MAX_CHARS: 2000;
|
|
41
|
+
DETAILS_MAX_CHARS: 20000;
|
|
42
|
+
CHOICES_MAX: 20;
|
|
43
|
+
CHOICE_MAX_CHARS: 200;
|
|
44
|
+
ANSWER_TEXT_MAX_CHARS: 20000;
|
|
45
|
+
REJECT_MESSAGE_MAX_CHARS: 4000;
|
|
46
|
+
TOOL_WAIT_DEFAULT_DEADLINE_SECONDS: 900;
|
|
47
|
+
TOOL_NAME_MAX_CHARS: 128;
|
|
48
|
+
TOOL_ERROR_MESSAGE_MAX_CHARS: 4000;
|
|
49
|
+
TASK_LEASE_DEFAULT_SECONDS: 7200;
|
|
50
|
+
}>;
|
|
51
|
+
export type Limits = typeof LIMITS;
|
|
52
|
+
//# sourceMappingURL=limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../../../src/workflow/limits.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4CjB,CAAC;AAEH,MAAM,MAAM,MAAM,GAAG,OAAO,MAAM,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LIMITS = void 0;
|
|
4
|
+
const KIB = 1024;
|
|
5
|
+
const MIB = 1024 * KIB;
|
|
6
|
+
/**
|
|
7
|
+
* The limits the platform enforces on an agent workflow (runner spec §2):
|
|
8
|
+
* backend-core's `INTEGRATIONS.WORKFLOW` values, every number and pattern of
|
|
9
|
+
* it, plus the lease's default length (`AGENT_POOL.TASK_LEASE_DEFAULT_SECONDS`,
|
|
10
|
+
* which agent pools §13 put in place of `SERVICE_LEASE_SECONDS` and
|
|
11
|
+
* `JOB_LEASE_DEFAULT_SECONDS`). The in-memory transport enforces them, and a
|
|
12
|
+
* turn can plan around them (declare fewer waits, keep `state` small).
|
|
13
|
+
*
|
|
14
|
+
* Core's contract test asserts its constants equal these (runner spec §6,
|
|
15
|
+
* change 3), so the two cannot drift without a failing test. Frozen, so no
|
|
16
|
+
* agent can loosen a limit for every other caller in the process.
|
|
17
|
+
*/
|
|
18
|
+
exports.LIMITS = Object.freeze({
|
|
19
|
+
// Record size (design §5.1).
|
|
20
|
+
STATE_MAX_BYTES: MIB,
|
|
21
|
+
INBOX_MAX: 200,
|
|
22
|
+
INBOX_EVENT_DATA_MAX_BYTES: 8 * KIB,
|
|
23
|
+
SIGNAL_INPUT_MAX_BYTES: 64 * KIB,
|
|
24
|
+
OPEN_WAITS_MAX: 50,
|
|
25
|
+
OPEN_INPUT_WAITS_MAX: 16,
|
|
26
|
+
OPEN_WAITS_MAX_BYTES: 4 * MIB,
|
|
27
|
+
SETTLED_WAITS_KEPT: 100,
|
|
28
|
+
CHECKPOINT_MAX_BYTES: 64 * MIB,
|
|
29
|
+
// Timing (W4).
|
|
30
|
+
QUEUED_RUN_RETRY_SECONDS: 60,
|
|
31
|
+
ENDED_RETENTION_DAYS: 30,
|
|
32
|
+
// Turn budget (W12).
|
|
33
|
+
MAX_TURNS: 1000,
|
|
34
|
+
MAX_TURNS_PER_HOUR: 60,
|
|
35
|
+
MIN_TIMER_SECONDS: 60,
|
|
36
|
+
INPUT_MAX_ATTEMPTS: 3,
|
|
37
|
+
WORKFLOW_SWEEP_PER_TENANT: 500,
|
|
38
|
+
// Names (§6.2).
|
|
39
|
+
KEY_PATTERN: /^[A-Za-z0-9._:-]{0,128}$/,
|
|
40
|
+
WAIT_TAG_PATTERN: /^[A-Za-z0-9._:-]{1,64}$/,
|
|
41
|
+
WAIT_FIELD_MAX_CHARS: 128,
|
|
42
|
+
CODE_MAX_CHARS: 256,
|
|
43
|
+
CONTACTS_MAX: 20,
|
|
44
|
+
ADDRESSEES_MAX: 20,
|
|
45
|
+
// Questions and answers (§6.1).
|
|
46
|
+
MAX_QUESTIONS_PER_WAIT: 20,
|
|
47
|
+
QUESTIONS_MAX_BYTES: 256 * KIB,
|
|
48
|
+
ANSWERS_MAX_BYTES: 256 * KIB,
|
|
49
|
+
TITLE_MAX_CHARS: 200,
|
|
50
|
+
QUESTION_MAX_CHARS: 2000,
|
|
51
|
+
DETAILS_MAX_CHARS: 20000,
|
|
52
|
+
CHOICES_MAX: 20,
|
|
53
|
+
CHOICE_MAX_CHARS: 200,
|
|
54
|
+
ANSWER_TEXT_MAX_CHARS: 20000,
|
|
55
|
+
REJECT_MESSAGE_MAX_CHARS: 4000,
|
|
56
|
+
// Tool waits (interactive tool calls, I2, §3.3).
|
|
57
|
+
TOOL_WAIT_DEFAULT_DEADLINE_SECONDS: 900,
|
|
58
|
+
TOOL_NAME_MAX_CHARS: 128,
|
|
59
|
+
TOOL_ERROR_MESSAGE_MAX_CHARS: 4000,
|
|
60
|
+
// The lease's length when the integration sets no task timeout (pools §13).
|
|
61
|
+
TASK_LEASE_DEFAULT_SECONDS: 7200,
|
|
62
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { integration as clientIntegration } from "@stackfactor/client-api";
|
|
2
|
+
import type { WorkflowTransport } from "./transport.js";
|
|
3
|
+
/** client-api's calls for an agent workflow (client-api `integration`, design §9). */
|
|
4
|
+
export type WorkflowCalls = Pick<typeof clientIntegration, "getWorkflow" | "leaseWorkflow" | "takeWorkflowInbox" | "saveWorkflowState" | "releaseWorkflow" | "endWorkflow" | "letGoWorkflow" | "reopenWorkflow" | "deleteWorkflow" | "cancelWorkflowWait" | "signalWorkflow" | "saveWorkflowCheckpoint" | "getWorkflowCheckpoint">;
|
|
5
|
+
/**
|
|
6
|
+
* A rejection as agent code may see it: the message, the method and path, and
|
|
7
|
+
* what retrying and the runner classify by (`isAxiosError`, a string `code`
|
|
8
|
+
* or a numeric structured one, `response.status`, and the platform's own
|
|
9
|
+
* `response.data.error` / `message`). Never `config` (the AGENT token, the
|
|
10
|
+
* request body), `request`, or the response's headers.
|
|
11
|
+
*/
|
|
12
|
+
export declare const sanitized: (error: any, operation: string) => Error;
|
|
13
|
+
/**
|
|
14
|
+
* The transport that calls the platform (runner spec §7): client-api's call
|
|
15
|
+
* for each agent route, with the run's AGENT token, which names the run and
|
|
16
|
+
* so its workflow. client-api resolves each route's answered 404s and 409s
|
|
17
|
+
* as their bodies; here each becomes the flag the runner reads, so no
|
|
18
|
+
* caller looks at an error code:
|
|
19
|
+
*
|
|
20
|
+
* - a 404 `AGENT_WORKFLOW_NOT_FOUND`: `get` answers null; `lease` and
|
|
21
|
+
* `reopen` answer `missing` (none, and none created; for a bound run, its
|
|
22
|
+
* workflow was deleted); every other route `gone` (an administrator
|
|
23
|
+
* deleted the workflow under the run, §6.2);
|
|
24
|
+
* - a 409 version mismatch: `versionMismatch`, for save, release and end;
|
|
25
|
+
* - `getCheckpoint`'s 404 `AGENT_WORKFLOW_NO_CHECKPOINT`: null.
|
|
26
|
+
*
|
|
27
|
+
* Every other answer passes through as the route sent it, and every
|
|
28
|
+
* rejection (401, 409 not holder, 413, 400, 422, a network failure) comes
|
|
29
|
+
* through for {@link retrying} and the runner to classify, as a
|
|
30
|
+
* {@link sanitized} error: an axios error carries the AGENT token
|
|
31
|
+
* (`config.headers.authorization`) and the request's body (the state, or the
|
|
32
|
+
* gzipped checkpoint with learner files), and the error flows to the agent's
|
|
33
|
+
* `onError`, to a rejected `checkpoint()` and to `main`'s rejection.
|
|
34
|
+
*
|
|
35
|
+
* @param token - The run's AGENT token (`request.authToken`)
|
|
36
|
+
* @param calls - client-api's calls; swappable in tests
|
|
37
|
+
*/
|
|
38
|
+
export declare const platformTransport: (token: string, calls?: WorkflowCalls) => WorkflowTransport;
|
|
39
|
+
//# sourceMappingURL=platformTransport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platformTransport.d.ts","sourceRoot":"","sources":["../../../src/workflow/platformTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE3E,OAAO,KAAK,EAaV,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAExB,sFAAsF;AACtF,MAAM,MAAM,aAAa,GAAG,IAAI,CAC9B,OAAO,iBAAiB,EACtB,aAAa,GACb,eAAe,GACf,mBAAmB,GACnB,mBAAmB,GACnB,iBAAiB,GACjB,aAAa,GACb,eAAe,GACf,gBAAgB,GAChB,gBAAgB,GAChB,oBAAoB,GACpB,gBAAgB,GAChB,wBAAwB,GACxB,uBAAuB,CAC1B,CAAC;AAWF;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,GAAG,EAAE,WAAW,MAAM,KAAG,KAiBzD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,iBAAiB,GAC5B,OAAO,MAAM,EACb,QAAO,aAAiC,KACvC,iBAgDF,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.platformTransport = exports.sanitized = void 0;
|
|
4
|
+
const client_api_1 = require("@stackfactor/client-api");
|
|
5
|
+
const transport_js_1 = require("./transport.js");
|
|
6
|
+
const NOT_FOUND = "AGENT_WORKFLOW_NOT_FOUND";
|
|
7
|
+
const NO_CHECKPOINT = "AGENT_WORKFLOW_NO_CHECKPOINT";
|
|
8
|
+
const VERSION_MISMATCH = "AGENT_WORKFLOW_VERSION_MISMATCH";
|
|
9
|
+
const errorOf = (answer) => answer?.error;
|
|
10
|
+
const textOf = (value) => typeof value === "string" ? value.slice(0, 500) : undefined;
|
|
11
|
+
/**
|
|
12
|
+
* A rejection as agent code may see it: the message, the method and path, and
|
|
13
|
+
* what retrying and the runner classify by (`isAxiosError`, a string `code`
|
|
14
|
+
* or a numeric structured one, `response.status`, and the platform's own
|
|
15
|
+
* `response.data.error` / `message`). Never `config` (the AGENT token, the
|
|
16
|
+
* request body), `request`, or the response's headers.
|
|
17
|
+
*/
|
|
18
|
+
const sanitized = (error, operation) => {
|
|
19
|
+
const method = textOf(error?.config?.method)?.toUpperCase();
|
|
20
|
+
const path = textOf(error?.config?.url);
|
|
21
|
+
const where = method && path ? `${method} ${path}` : operation;
|
|
22
|
+
const out = new Error(`${textOf(error?.message) ?? "the platform call failed"} (${where})`);
|
|
23
|
+
out.name = error?.name === "AxiosError" ? "AxiosError" : "WorkflowTransportError";
|
|
24
|
+
if (error?.isAxiosError)
|
|
25
|
+
out.isAxiosError = true;
|
|
26
|
+
if (typeof error?.code === "string" || typeof error?.code === "number")
|
|
27
|
+
out.code = error.code;
|
|
28
|
+
const status = error?.response?.status;
|
|
29
|
+
if (typeof status === "number") {
|
|
30
|
+
const data = error.response.data;
|
|
31
|
+
out.response = {
|
|
32
|
+
status,
|
|
33
|
+
data: { ...(textOf(data?.error) ? { error: textOf(data.error) } : {}), ...(textOf(data?.message) ? { message: textOf(data.message) } : {}) },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
};
|
|
38
|
+
exports.sanitized = sanitized;
|
|
39
|
+
/**
|
|
40
|
+
* The transport that calls the platform (runner spec §7): client-api's call
|
|
41
|
+
* for each agent route, with the run's AGENT token, which names the run and
|
|
42
|
+
* so its workflow. client-api resolves each route's answered 404s and 409s
|
|
43
|
+
* as their bodies; here each becomes the flag the runner reads, so no
|
|
44
|
+
* caller looks at an error code:
|
|
45
|
+
*
|
|
46
|
+
* - a 404 `AGENT_WORKFLOW_NOT_FOUND`: `get` answers null; `lease` and
|
|
47
|
+
* `reopen` answer `missing` (none, and none created; for a bound run, its
|
|
48
|
+
* workflow was deleted); every other route `gone` (an administrator
|
|
49
|
+
* deleted the workflow under the run, §6.2);
|
|
50
|
+
* - a 409 version mismatch: `versionMismatch`, for save, release and end;
|
|
51
|
+
* - `getCheckpoint`'s 404 `AGENT_WORKFLOW_NO_CHECKPOINT`: null.
|
|
52
|
+
*
|
|
53
|
+
* Every other answer passes through as the route sent it, and every
|
|
54
|
+
* rejection (401, 409 not holder, 413, 400, 422, a network failure) comes
|
|
55
|
+
* through for {@link retrying} and the runner to classify, as a
|
|
56
|
+
* {@link sanitized} error: an axios error carries the AGENT token
|
|
57
|
+
* (`config.headers.authorization`) and the request's body (the state, or the
|
|
58
|
+
* gzipped checkpoint with learner files), and the error flows to the agent's
|
|
59
|
+
* `onError`, to a rejected `checkpoint()` and to `main`'s rejection.
|
|
60
|
+
*
|
|
61
|
+
* @param token - The run's AGENT token (`request.authToken`)
|
|
62
|
+
* @param calls - client-api's calls; swappable in tests
|
|
63
|
+
*/
|
|
64
|
+
const platformTransport = (token, calls = client_api_1.integration) => {
|
|
65
|
+
const gone = (answer) => (errorOf(answer) === NOT_FOUND ? { gone: true } : answer);
|
|
66
|
+
const written = (answer, flag) => {
|
|
67
|
+
if (errorOf(answer) === NOT_FOUND)
|
|
68
|
+
return { gone: true };
|
|
69
|
+
if (errorOf(answer) === VERSION_MISMATCH)
|
|
70
|
+
return { [flag]: false, versionMismatch: true };
|
|
71
|
+
return answer;
|
|
72
|
+
};
|
|
73
|
+
const raw = {
|
|
74
|
+
get: async (key) => {
|
|
75
|
+
const answer = await calls.getWorkflow(key, token);
|
|
76
|
+
return errorOf(answer) === NOT_FOUND ? null : answer;
|
|
77
|
+
},
|
|
78
|
+
lease: async (body) => {
|
|
79
|
+
const answer = await calls.leaseWorkflow(body, token);
|
|
80
|
+
return (errorOf(answer) === NOT_FOUND ? { taken: false, missing: true } : answer);
|
|
81
|
+
},
|
|
82
|
+
take: async () => gone(await calls.takeWorkflowInbox(token)),
|
|
83
|
+
save: async (body) => written(await calls.saveWorkflowState(body, token), "saved"),
|
|
84
|
+
release: async (body) => written(await calls.releaseWorkflow(body, token), "released"),
|
|
85
|
+
end: async (body) => written(await calls.endWorkflow(body, token), "ended"),
|
|
86
|
+
letGo: async () => gone(await calls.letGoWorkflow(token)),
|
|
87
|
+
reopen: async () => {
|
|
88
|
+
const answer = await calls.reopenWorkflow(token);
|
|
89
|
+
return (errorOf(answer) === NOT_FOUND ? { reopened: false, missing: true } : answer);
|
|
90
|
+
},
|
|
91
|
+
delete: async () => gone(await calls.deleteWorkflow(token)),
|
|
92
|
+
cancelWait: async (waitId) => gone(await calls.cancelWorkflowWait(waitId, token)),
|
|
93
|
+
signal: async (body) => await calls.signalWorkflow(body, token),
|
|
94
|
+
saveCheckpoint: async (bytes) => gone(await calls.saveWorkflowCheckpoint(bytes, token)),
|
|
95
|
+
getCheckpoint: async () => {
|
|
96
|
+
const answer = await calls.getWorkflowCheckpoint(token);
|
|
97
|
+
if (errorOf(answer) === NO_CHECKPOINT)
|
|
98
|
+
return null;
|
|
99
|
+
return gone(answer);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
const guarded = {};
|
|
103
|
+
for (const name of transport_js_1.WORKFLOW_OPERATIONS) {
|
|
104
|
+
guarded[name] = async (...args) => {
|
|
105
|
+
try {
|
|
106
|
+
return await raw[name](...args);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw (0, exports.sanitized)(error, name);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return guarded;
|
|
114
|
+
};
|
|
115
|
+
exports.platformTransport = platformTransport;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { WorkflowTransport } from "./transport.js";
|
|
2
|
+
export interface RetryOptions {
|
|
3
|
+
/** Retries after the first attempt. Default 3. */
|
|
4
|
+
retries?: number;
|
|
5
|
+
/** The first retry's wait; each later one doubles it. Default 500 ms. */
|
|
6
|
+
baseMs?: number;
|
|
7
|
+
/** How to wait; swappable in tests. */
|
|
8
|
+
sleep?: (ms: number) => Promise<void>;
|
|
9
|
+
/** Jitter source in [0, 1); swappable in tests. */
|
|
10
|
+
random?: () => number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* True for a failure that is no answer at all: a 5xx, or a network error (no
|
|
14
|
+
* HTTP response, and either an axios error or a string error code such as
|
|
15
|
+
* `ECONNRESET`). Every 4xx is an answer, 429 and 408 included (runner spec
|
|
16
|
+
* §7: "4xx never retried"), and so is an error with neither signature (a bug,
|
|
17
|
+
* or client-api refusing a body before any request), and one whose `code` is
|
|
18
|
+
* a number (errorHandling's structured errors carry their HTTP status there).
|
|
19
|
+
*/
|
|
20
|
+
export declare const isTransient: (error: any) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* What a log line may say about a failed call: its HTTP status or its error
|
|
23
|
+
* code, never the error itself, whose message, config and request carry the
|
|
24
|
+
* run's token and, for a write, the whole state.
|
|
25
|
+
*/
|
|
26
|
+
export declare const failureOf: (error: any) => string;
|
|
27
|
+
/**
|
|
28
|
+
* `transport` with each call made again while it fails transiently (runner
|
|
29
|
+
* spec §7, lifted from agent-utils #60's `retryingTransport`): network errors
|
|
30
|
+
* and 5xx answers are retried `retries` times (3), waiting
|
|
31
|
+
* `baseMs`·2^(n−1) before retry n (500 ms, 1 s, 2 s) plus up to half that
|
|
32
|
+
* again at random, so the runs of one burst do not retry in the same
|
|
33
|
+
* instant. Answers are never retried, whether resolved or rejected (401, 4xx,
|
|
34
|
+
* the answered 404s and 409s). After the last retry the last error is
|
|
35
|
+
* rethrown unchanged.
|
|
36
|
+
*
|
|
37
|
+
* Every call is safe to make again (runner spec §7): a lease and a take
|
|
38
|
+
* answer a holder that asks twice with the same answer; let-go, delete,
|
|
39
|
+
* cancel and signal answer a repeat with the same outcome; and a save,
|
|
40
|
+
* release or end whose first try landed is recognised by the runner from its
|
|
41
|
+
* last-write stamp.
|
|
42
|
+
*/
|
|
43
|
+
export declare const retrying: (transport: WorkflowTransport, options?: RetryOptions) => WorkflowTransport;
|
|
44
|
+
//# sourceMappingURL=retrying.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrying.d.ts","sourceRoot":"","sources":["../../../src/workflow/retrying.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,MAAM,WAAW,YAAY;IAC3B,kDAAkD;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yEAAyE;IACzE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,mDAAmD;IACnD,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;CACvB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,GAAG,KAAG,OAIxC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,GAAG,KAAG,MAKtC,CAAC;AAIF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,QAAQ,GAAI,WAAW,iBAAiB,EAAE,UAAS,YAAiB,KAAG,iBA6BnF,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
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.retrying = exports.failureOf = exports.isTransient = void 0;
|
|
7
|
+
const logger_js_1 = __importDefault(require("../logger.js"));
|
|
8
|
+
const transport_js_1 = require("./transport.js");
|
|
9
|
+
/**
|
|
10
|
+
* True for a failure that is no answer at all: a 5xx, or a network error (no
|
|
11
|
+
* HTTP response, and either an axios error or a string error code such as
|
|
12
|
+
* `ECONNRESET`). Every 4xx is an answer, 429 and 408 included (runner spec
|
|
13
|
+
* §7: "4xx never retried"), and so is an error with neither signature (a bug,
|
|
14
|
+
* or client-api refusing a body before any request), and one whose `code` is
|
|
15
|
+
* a number (errorHandling's structured errors carry their HTTP status there).
|
|
16
|
+
*/
|
|
17
|
+
const isTransient = (error) => {
|
|
18
|
+
const status = error?.response?.status;
|
|
19
|
+
if (typeof status === "number")
|
|
20
|
+
return status >= 500;
|
|
21
|
+
return Boolean(error?.isAxiosError) || typeof error?.code === "string";
|
|
22
|
+
};
|
|
23
|
+
exports.isTransient = isTransient;
|
|
24
|
+
/**
|
|
25
|
+
* What a log line may say about a failed call: its HTTP status or its error
|
|
26
|
+
* code, never the error itself, whose message, config and request carry the
|
|
27
|
+
* run's token and, for a write, the whole state.
|
|
28
|
+
*/
|
|
29
|
+
const failureOf = (error) => {
|
|
30
|
+
const status = error?.response?.status;
|
|
31
|
+
if (typeof status === "number")
|
|
32
|
+
return `HTTP ${status}`;
|
|
33
|
+
if (typeof error?.code === "string")
|
|
34
|
+
return error.code;
|
|
35
|
+
return "no response";
|
|
36
|
+
};
|
|
37
|
+
exports.failureOf = failureOf;
|
|
38
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
39
|
+
/**
|
|
40
|
+
* `transport` with each call made again while it fails transiently (runner
|
|
41
|
+
* spec §7, lifted from agent-utils #60's `retryingTransport`): network errors
|
|
42
|
+
* and 5xx answers are retried `retries` times (3), waiting
|
|
43
|
+
* `baseMs`·2^(n−1) before retry n (500 ms, 1 s, 2 s) plus up to half that
|
|
44
|
+
* again at random, so the runs of one burst do not retry in the same
|
|
45
|
+
* instant. Answers are never retried, whether resolved or rejected (401, 4xx,
|
|
46
|
+
* the answered 404s and 409s). After the last retry the last error is
|
|
47
|
+
* rethrown unchanged.
|
|
48
|
+
*
|
|
49
|
+
* Every call is safe to make again (runner spec §7): a lease and a take
|
|
50
|
+
* answer a holder that asks twice with the same answer; let-go, delete,
|
|
51
|
+
* cancel and signal answer a repeat with the same outcome; and a save,
|
|
52
|
+
* release or end whose first try landed is recognised by the runner from its
|
|
53
|
+
* last-write stamp.
|
|
54
|
+
*/
|
|
55
|
+
const retrying = (transport, options = {}) => {
|
|
56
|
+
const retries = Math.max(0, options.retries ?? 3);
|
|
57
|
+
const baseMs = options.baseMs ?? 500;
|
|
58
|
+
const sleep = options.sleep ?? delay;
|
|
59
|
+
const random = options.random ?? Math.random;
|
|
60
|
+
const attempts = retries + 1;
|
|
61
|
+
const call = async (name, make) => {
|
|
62
|
+
for (let attempt = 1;; attempt++) {
|
|
63
|
+
try {
|
|
64
|
+
return await make();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (attempt >= attempts || !(0, exports.isTransient)(error))
|
|
68
|
+
throw error;
|
|
69
|
+
const backoff = baseMs * 2 ** (attempt - 1);
|
|
70
|
+
const wait = Math.floor(backoff + (random() * backoff) / 2);
|
|
71
|
+
logger_js_1.default.log(null, logger_js_1.default.levels.warn, `workflow: the platform's ${name} failed (${(0, exports.failureOf)(error)}) on attempt ${attempt} of ${attempts}; trying again in ${wait}ms`);
|
|
72
|
+
await sleep(wait);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const wrapped = {};
|
|
77
|
+
for (const name of transport_js_1.WORKFLOW_OPERATIONS) {
|
|
78
|
+
wrapped[name] = (...args) => call(name, () => transport[name](...args));
|
|
79
|
+
}
|
|
80
|
+
return wrapped;
|
|
81
|
+
};
|
|
82
|
+
exports.retrying = retrying;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { AgentMain } from "../serve.js";
|
|
2
|
+
import type { OutboxEntry } from "./bookkeeping.js";
|
|
3
|
+
import type { InteractiveInput, OpenWait, PlatformInput, WaitDecl, Workflow, WorkflowTransport } from "./transport.js";
|
|
4
|
+
/** `decide`'s answers: run the input, drop it, reopen or create the workflow for it, or delete the workflow. */
|
|
5
|
+
export type Decision = "run" | "ignore" | "reopen" | "create" | "delete";
|
|
6
|
+
export type { InteractiveInput };
|
|
7
|
+
/**
|
|
8
|
+
* What a turn is handed: an `event` or a `resume` as the platform hands it
|
|
9
|
+
* (§6.3), or an interactive request: an INTERACTIVE run's own (its `id` the
|
|
10
|
+
* run's) or one taken from the inbox (its `id` the entry's).
|
|
11
|
+
*/
|
|
12
|
+
export type WorkflowInput = PlatformInput;
|
|
13
|
+
/**
|
|
14
|
+
* What happens after a turn. `{}` releases with no change to waits;
|
|
15
|
+
* `{ end: true }` ends the workflow; `{ delete: true }` deletes it.
|
|
16
|
+
*/
|
|
17
|
+
export interface Next {
|
|
18
|
+
/** Waits to declare, written with the turn's `done` (§6.2). */
|
|
19
|
+
wait?: WaitDecl[];
|
|
20
|
+
/** Open waits to cancel. One not in the answer's `cancelled` had already settled: its resume will come. */
|
|
21
|
+
cancel?: string[];
|
|
22
|
+
/** Who is told of a halt or a failed input (§6.2); the latest turn that names them wins. */
|
|
23
|
+
contacts?: string[];
|
|
24
|
+
/**
|
|
25
|
+
* Whether to wait for a quick resolution of this turn's waits (§4). Default:
|
|
26
|
+
* true when the turn declared an `input` wait, else false. Waiting happens
|
|
27
|
+
* only while `options.linger.maxMs` is above 0 (off by default, pools §13).
|
|
28
|
+
*/
|
|
29
|
+
linger?: boolean;
|
|
30
|
+
end?: boolean;
|
|
31
|
+
delete?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** A turn's answer: the agent's next state and what happens next. */
|
|
34
|
+
export interface TurnResult {
|
|
35
|
+
state: unknown;
|
|
36
|
+
next?: Next;
|
|
37
|
+
/**
|
|
38
|
+
* The run's outcome (I6): when this turn's `next` is `{ end: true }` and it
|
|
39
|
+
* is the run's last, `main` returns `{ …summary, result }`, or for a round
|
|
40
|
+
* of an interactive workflow the result itself (Ruling 22), and `serve()` /
|
|
41
|
+
* `runOnce()` settle the run with it. Ignored when the turn does not end
|
|
42
|
+
* the workflow. JSON, bounded by the settle route's inline limit.
|
|
43
|
+
*/
|
|
44
|
+
result?: unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface CheckpointOptions {
|
|
47
|
+
/** Waits declared mid-turn, without `done` (a `signal` wait before the tool gets its correlation, §7.6). */
|
|
48
|
+
waits?: WaitDecl[];
|
|
49
|
+
cancel?: string[];
|
|
50
|
+
contacts?: string[];
|
|
51
|
+
/** Also send the outbox under the lease now, and save again with the sent entries removed. */
|
|
52
|
+
send?: boolean;
|
|
53
|
+
}
|
|
54
|
+
export type CheckpointFn = (state: unknown, options?: CheckpointOptions) => Promise<{
|
|
55
|
+
waits: {
|
|
56
|
+
waitId: string;
|
|
57
|
+
}[];
|
|
58
|
+
cancelled: string[];
|
|
59
|
+
}>;
|
|
60
|
+
/** Notifications a turn queues; each `key` is the same however often the turn runs. */
|
|
61
|
+
export interface Outbox {
|
|
62
|
+
add(entries: OutboxEntry | OutboxEntry[]): void;
|
|
63
|
+
}
|
|
64
|
+
export interface TurnArgs {
|
|
65
|
+
input: WorkflowInput;
|
|
66
|
+
/** `decide`'s answer for the run's own input; null for an inbox entry and for a resume run's own input. */
|
|
67
|
+
decision: Decision | null;
|
|
68
|
+
/** The agent's state: never the runner's bookkeeping. */
|
|
69
|
+
state: unknown;
|
|
70
|
+
checkpoint: CheckpointFn;
|
|
71
|
+
outbox: Outbox;
|
|
72
|
+
/** The workflow's open waits, as this run knows them. */
|
|
73
|
+
openWaits: OpenWait[];
|
|
74
|
+
}
|
|
75
|
+
export interface Handlers {
|
|
76
|
+
/** Which workflow the run's own input belongs to, and whether a missing one may be created. Default: key "", no create. */
|
|
77
|
+
open?: (input: WorkflowInput) => {
|
|
78
|
+
key?: string;
|
|
79
|
+
create?: boolean;
|
|
80
|
+
} | Promise<{
|
|
81
|
+
key?: string;
|
|
82
|
+
create?: boolean;
|
|
83
|
+
}>;
|
|
84
|
+
/** Before the lease, with the workflow as read (or null). Must be total; a throw is logged and taken as "run". */
|
|
85
|
+
decide?: (args: {
|
|
86
|
+
input: WorkflowInput;
|
|
87
|
+
workflow: Workflow | null;
|
|
88
|
+
}) => Decision | Promise<Decision>;
|
|
89
|
+
/** One input under the lease. */
|
|
90
|
+
turn: (args: TurnArgs) => TurnResult | Promise<TurnResult>;
|
|
91
|
+
/** A turn that threw. Default: let-go, so the platform runs the input again from the last saved state. */
|
|
92
|
+
onError?: (args: {
|
|
93
|
+
input: WorkflowInput;
|
|
94
|
+
error: unknown;
|
|
95
|
+
state: unknown;
|
|
96
|
+
/** Gives the lease up, re-queuing the input; what the default does. */
|
|
97
|
+
letGo: () => Promise<void>;
|
|
98
|
+
}) => void | Promise<void>;
|
|
99
|
+
/** Sends one outbox entry. Required once a turn adds one; receivers deduplicate on its key. */
|
|
100
|
+
send?: (entry: OutboxEntry) => void | Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
export interface LingerOptions {
|
|
103
|
+
/** How long after a turn to keep waiting for its waits to resolve. Default 0: off (pools §13). */
|
|
104
|
+
maxMs?: number;
|
|
105
|
+
/** How often to take the inbox while waiting. Default 5000. */
|
|
106
|
+
pollMs?: number;
|
|
107
|
+
}
|
|
108
|
+
export interface Clock {
|
|
109
|
+
now(): number;
|
|
110
|
+
/** Waits `ms`, or less when `signal` aborts. */
|
|
111
|
+
sleep(ms: number, signal?: AbortSignal | null): Promise<void>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Where the run's own input comes from. The default, {@link messageAdapter},
|
|
115
|
+
* reads the run message `serve()` and `runOnce()` bind as `params.data`; a
|
|
116
|
+
* pool worker will supply its claimed task's payload instead (pools §13).
|
|
117
|
+
*/
|
|
118
|
+
export interface RunStart {
|
|
119
|
+
kind: "event" | "interactive" | "resume" | "inbox";
|
|
120
|
+
/** The run's own id (its batch job). */
|
|
121
|
+
batchJobId: string | null;
|
|
122
|
+
/** Bound to its workflow from its creation (a resume or queued-input run): `open` is not consulted, no key is sent. */
|
|
123
|
+
bound: boolean;
|
|
124
|
+
/** The run's own input as known before the lease, for `open` and `decide`; null when it has none of its own here. */
|
|
125
|
+
input: WorkflowInput | null;
|
|
126
|
+
}
|
|
127
|
+
export type InputAdapter = (params: any, request: any) => RunStart;
|
|
128
|
+
export interface RunOptions {
|
|
129
|
+
linger?: LingerOptions;
|
|
130
|
+
/** Default: `retrying(platformTransport(request.authToken))`. */
|
|
131
|
+
transport?: WorkflowTransport;
|
|
132
|
+
adapter?: InputAdapter;
|
|
133
|
+
clock?: Clock;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* How the run ended: `released`, `ended` and `deleted` say how this run let
|
|
137
|
+
* the workflow go; `deferred`, `handled`, `missing`, `already-ended` and
|
|
138
|
+
* `halted` are the lease's answers (`halted` also when a later take answered
|
|
139
|
+
* it, the run then releasing what it had); `ignored` is `decide`'s answer;
|
|
140
|
+
* `gone` means an administrator deleted the workflow.
|
|
141
|
+
*/
|
|
142
|
+
export type RunOutcome = "released" | "ended" | "deleted" | "deferred" | "handled" | "missing" | "already-ended" | "halted" | "ignored" | "gone";
|
|
143
|
+
/**
|
|
144
|
+
* What `main` returns: logged, and the run's result. A run whose outcome is a
|
|
145
|
+
* caller's answer (an INTERACTIVE run, or any round of a workflow an
|
|
146
|
+
* interactive call created, as its lease answer says) returns only `result`
|
|
147
|
+
* (Ruling 22); its summary is logged.
|
|
148
|
+
*/
|
|
149
|
+
export interface RunSummary {
|
|
150
|
+
workflowId: string | null;
|
|
151
|
+
outcome: RunOutcome;
|
|
152
|
+
turns: number;
|
|
153
|
+
lingered: {
|
|
154
|
+
ms: number;
|
|
155
|
+
resolved: boolean;
|
|
156
|
+
} | null;
|
|
157
|
+
/**
|
|
158
|
+
* The last turn's `result`, when that turn answered `{ end: true }` and the
|
|
159
|
+
* run let the workflow go accordingly: `ended`, or `halted` (the end
|
|
160
|
+
* released instead, W4). Absent otherwise.
|
|
161
|
+
*/
|
|
162
|
+
result?: unknown;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The run's own input from its run message (runner spec §3 "How it plugs
|
|
166
|
+
* in"): `params.data` is the run record's `steps[0].data` as backend-core's
|
|
167
|
+
* `getExecutionEnvelope` builds it, the same for a SERVICE and a JOB run.
|
|
168
|
+
* `trigger: "RESUME"` is a resume run, bound from its creation; its input (the
|
|
169
|
+
* wait's outcome) is not in the message and comes from the lease. `trigger:
|
|
170
|
+
* "INBOX"` is a queued-input run, with no input of its own. `trigger:
|
|
171
|
+
* "INTERACTIVE"` (or `params.contentType` "INTERACTIVE") is a synchronous
|
|
172
|
+
* agent call made a run (I1): its input is `{ type: "interactive", id: <run
|
|
173
|
+
* id>, payload: params.data.payload }`, what the caller asked for. Anything
|
|
174
|
+
* else is a platform event (a scheduled run's included). Before the lease an
|
|
175
|
+
* event has no inbox entry, so its `id` is the run's own; the turn is handed
|
|
176
|
+
* the platform's input, whose `id` is its entry.
|
|
177
|
+
*
|
|
178
|
+
* An INTERACTIVE run arrives claimed by the platform, as every run reaches
|
|
179
|
+
* `main` claimed (by `serve()`'s caller or by `runOnce()`): the runner never
|
|
180
|
+
* claims.
|
|
181
|
+
*/
|
|
182
|
+
export declare const messageAdapter: InputAdapter;
|
|
183
|
+
/**
|
|
184
|
+
* `workflow.run(handlers, options)` (runner spec §3): an ordinary agent
|
|
185
|
+
* `main`, so `serve()` (SERVICE) and `runOnce()` (JOB) run it as they run any
|
|
186
|
+
* agent, and settle the run with what it returns. The agent writes `decide`
|
|
187
|
+
* and `turn`; the runner owns the lease, the inbox loop one entry per take,
|
|
188
|
+
* the end-of-turn saves with the outbox sent under the lease, release, end
|
|
189
|
+
* and delete, let-go on a failed turn, the last-write stamp, and the wait
|
|
190
|
+
* for a quick resolution (off unless `linger.maxMs` is set, and never past
|
|
191
|
+
* the lease's `until`). The runner never claims the run: `serve()` and
|
|
192
|
+
* `runOnce()` own its lifecycle.
|
|
193
|
+
*/
|
|
194
|
+
export declare const run: (handlers: Handlers, options?: RunOptions) => AgentMain;
|
|
195
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../../src/workflow/run.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAA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