@arke-institute/rhiza 0.1.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 +106 -0
- package/dist/__tests__/fixtures/index.d.ts +9 -0
- package/dist/__tests__/fixtures/index.d.ts.map +1 -0
- package/dist/__tests__/fixtures/index.js +12 -0
- package/dist/__tests__/fixtures/index.js.map +1 -0
- package/dist/__tests__/fixtures/kladoi/index.d.ts +165 -0
- package/dist/__tests__/fixtures/kladoi/index.d.ts.map +1 -0
- package/dist/__tests__/fixtures/kladoi/index.js +270 -0
- package/dist/__tests__/fixtures/kladoi/index.js.map +1 -0
- package/dist/__tests__/fixtures/logs/index.d.ts +19 -0
- package/dist/__tests__/fixtures/logs/index.d.ts.map +1 -0
- package/dist/__tests__/fixtures/logs/index.js +545 -0
- package/dist/__tests__/fixtures/logs/index.js.map +1 -0
- package/dist/__tests__/fixtures/mock-client.d.ts +127 -0
- package/dist/__tests__/fixtures/mock-client.d.ts.map +1 -0
- package/dist/__tests__/fixtures/mock-client.js +415 -0
- package/dist/__tests__/fixtures/mock-client.js.map +1 -0
- package/dist/__tests__/fixtures/rhizai/index.d.ts +54 -0
- package/dist/__tests__/fixtures/rhizai/index.d.ts.map +1 -0
- package/dist/__tests__/fixtures/rhizai/index.js +283 -0
- package/dist/__tests__/fixtures/rhizai/index.js.map +1 -0
- package/dist/__tests__/unit/fixtures.test.d.ts +10 -0
- package/dist/__tests__/unit/fixtures.test.d.ts.map +1 -0
- package/dist/__tests__/unit/fixtures.test.js +275 -0
- package/dist/__tests__/unit/fixtures.test.js.map +1 -0
- package/dist/__tests__/unit/handoff/gather.test.d.ts +8 -0
- package/dist/__tests__/unit/handoff/gather.test.d.ts.map +1 -0
- package/dist/__tests__/unit/handoff/gather.test.js +176 -0
- package/dist/__tests__/unit/handoff/gather.test.js.map +1 -0
- package/dist/__tests__/unit/handoff/interpret.test.d.ts +9 -0
- package/dist/__tests__/unit/handoff/interpret.test.d.ts.map +1 -0
- package/dist/__tests__/unit/handoff/interpret.test.js +370 -0
- package/dist/__tests__/unit/handoff/interpret.test.js.map +1 -0
- package/dist/__tests__/unit/handoff/route.test.d.ts +8 -0
- package/dist/__tests__/unit/handoff/route.test.d.ts.map +1 -0
- package/dist/__tests__/unit/handoff/route.test.js +271 -0
- package/dist/__tests__/unit/handoff/route.test.js.map +1 -0
- package/dist/__tests__/unit/handoff/scatter.test.d.ts +7 -0
- package/dist/__tests__/unit/handoff/scatter.test.d.ts.map +1 -0
- package/dist/__tests__/unit/handoff/scatter.test.js +54 -0
- package/dist/__tests__/unit/handoff/scatter.test.js.map +1 -0
- package/dist/__tests__/unit/resume.test.d.ts +8 -0
- package/dist/__tests__/unit/resume.test.d.ts.map +1 -0
- package/dist/__tests__/unit/resume.test.js +134 -0
- package/dist/__tests__/unit/resume.test.js.map +1 -0
- package/dist/__tests__/unit/status.test.d.ts +8 -0
- package/dist/__tests__/unit/status.test.d.ts.map +1 -0
- package/dist/__tests__/unit/status.test.js +164 -0
- package/dist/__tests__/unit/status.test.js.map +1 -0
- package/dist/__tests__/unit/target.test.d.ts +8 -0
- package/dist/__tests__/unit/target.test.d.ts.map +1 -0
- package/dist/__tests__/unit/target.test.js +116 -0
- package/dist/__tests__/unit/target.test.js.map +1 -0
- package/dist/__tests__/unit/traverse.test.d.ts +8 -0
- package/dist/__tests__/unit/traverse.test.d.ts.map +1 -0
- package/dist/__tests__/unit/traverse.test.js +143 -0
- package/dist/__tests__/unit/traverse.test.js.map +1 -0
- package/dist/__tests__/unit/validation/klados.test.d.ts +16 -0
- package/dist/__tests__/unit/validation/klados.test.d.ts.map +1 -0
- package/dist/__tests__/unit/validation/klados.test.js +275 -0
- package/dist/__tests__/unit/validation/klados.test.js.map +1 -0
- package/dist/__tests__/unit/validation/rhiza.test.d.ts +16 -0
- package/dist/__tests__/unit/validation/rhiza.test.d.ts.map +1 -0
- package/dist/__tests__/unit/validation/rhiza.test.js +612 -0
- package/dist/__tests__/unit/validation/rhiza.test.js.map +1 -0
- package/dist/__tests__/unit/validation/runtime.test.d.ts +11 -0
- package/dist/__tests__/unit/validation/runtime.test.d.ts.map +1 -0
- package/dist/__tests__/unit/validation/runtime.test.js +553 -0
- package/dist/__tests__/unit/validation/runtime.test.js.map +1 -0
- package/dist/__tests__/unit/worker/errors.test.d.ts +2 -0
- package/dist/__tests__/unit/worker/errors.test.d.ts.map +1 -0
- package/dist/__tests__/unit/worker/errors.test.js +226 -0
- package/dist/__tests__/unit/worker/errors.test.js.map +1 -0
- package/dist/__tests__/unit/worker/job.test.d.ts +2 -0
- package/dist/__tests__/unit/worker/job.test.d.ts.map +1 -0
- package/dist/__tests__/unit/worker/job.test.js +233 -0
- package/dist/__tests__/unit/worker/job.test.js.map +1 -0
- package/dist/client/index.d.ts +10 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +8 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/interface.d.ts +142 -0
- package/dist/client/interface.d.ts.map +1 -0
- package/dist/client/interface.js +8 -0
- package/dist/client/interface.js.map +1 -0
- package/dist/client/mock.d.ts +95 -0
- package/dist/client/mock.d.ts.map +1 -0
- package/dist/client/mock.js +795 -0
- package/dist/client/mock.js.map +1 -0
- package/dist/client/types.d.ts +344 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +8 -0
- package/dist/client/types.js.map +1 -0
- package/dist/handoff/gather-api.d.ts +60 -0
- package/dist/handoff/gather-api.d.ts.map +1 -0
- package/dist/handoff/gather-api.js +130 -0
- package/dist/handoff/gather-api.js.map +1 -0
- package/dist/handoff/gather.d.ts +59 -0
- package/dist/handoff/gather.d.ts.map +1 -0
- package/dist/handoff/gather.js +134 -0
- package/dist/handoff/gather.js.map +1 -0
- package/dist/handoff/index.d.ts +19 -0
- package/dist/handoff/index.d.ts.map +1 -0
- package/dist/handoff/index.js +25 -0
- package/dist/handoff/index.js.map +1 -0
- package/dist/handoff/interpret.d.ts +79 -0
- package/dist/handoff/interpret.d.ts.map +1 -0
- package/dist/handoff/interpret.js +197 -0
- package/dist/handoff/interpret.js.map +1 -0
- package/dist/handoff/invoke.d.ts +82 -0
- package/dist/handoff/invoke.d.ts.map +1 -0
- package/dist/handoff/invoke.js +196 -0
- package/dist/handoff/invoke.js.map +1 -0
- package/dist/handoff/route.d.ts +25 -0
- package/dist/handoff/route.d.ts.map +1 -0
- package/dist/handoff/route.js +65 -0
- package/dist/handoff/route.js.map +1 -0
- package/dist/handoff/scatter-api.d.ts +62 -0
- package/dist/handoff/scatter-api.d.ts.map +1 -0
- package/dist/handoff/scatter-api.js +81 -0
- package/dist/handoff/scatter-api.js.map +1 -0
- package/dist/handoff/scatter.d.ts +19 -0
- package/dist/handoff/scatter.d.ts.map +1 -0
- package/dist/handoff/scatter.js +27 -0
- package/dist/handoff/scatter.js.map +1 -0
- package/dist/handoff/target.d.ts +16 -0
- package/dist/handoff/target.d.ts.map +1 -0
- package/dist/handoff/target.js +51 -0
- package/dist/handoff/target.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/index.d.ts +4 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +3 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/logger.d.ts +52 -0
- package/dist/logging/logger.d.ts.map +1 -0
- package/dist/logging/logger.js +70 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/logging/writer.d.ts +63 -0
- package/dist/logging/writer.d.ts.map +1 -0
- package/dist/logging/writer.js +160 -0
- package/dist/logging/writer.js.map +1 -0
- package/dist/resume/index.d.ts +77 -0
- package/dist/resume/index.d.ts.map +1 -0
- package/dist/resume/index.js +110 -0
- package/dist/resume/index.js.map +1 -0
- package/dist/status/index.d.ts +54 -0
- package/dist/status/index.d.ts.map +1 -0
- package/dist/status/index.js +107 -0
- package/dist/status/index.js.map +1 -0
- package/dist/traverse/index.d.ts +53 -0
- package/dist/traverse/index.d.ts.map +1 -0
- package/dist/traverse/index.js +142 -0
- package/dist/traverse/index.js.map +1 -0
- package/dist/types/batch.d.ts +53 -0
- package/dist/types/batch.d.ts.map +1 -0
- package/dist/types/batch.js +2 -0
- package/dist/types/batch.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/klados.d.ts +63 -0
- package/dist/types/klados.d.ts.map +1 -0
- package/dist/types/klados.js +2 -0
- package/dist/types/klados.js.map +1 -0
- package/dist/types/log.d.ts +107 -0
- package/dist/types/log.d.ts.map +1 -0
- package/dist/types/log.js +2 -0
- package/dist/types/log.js.map +1 -0
- package/dist/types/refs.d.ts +58 -0
- package/dist/types/refs.d.ts.map +1 -0
- package/dist/types/refs.js +43 -0
- package/dist/types/refs.js.map +1 -0
- package/dist/types/request.d.ts +69 -0
- package/dist/types/request.d.ts.map +1 -0
- package/dist/types/request.js +2 -0
- package/dist/types/request.js.map +1 -0
- package/dist/types/response.d.ts +31 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +2 -0
- package/dist/types/response.js.map +1 -0
- package/dist/types/rhiza.d.ts +100 -0
- package/dist/types/rhiza.d.ts.map +1 -0
- package/dist/types/rhiza.js +2 -0
- package/dist/types/rhiza.js.map +1 -0
- package/dist/types/status.d.ts +84 -0
- package/dist/types/status.d.ts.map +1 -0
- package/dist/types/status.js +2 -0
- package/dist/types/status.js.map +1 -0
- package/dist/utils/id.d.ts +15 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/id.js +25 -0
- package/dist/utils/id.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/validation/index.d.ts +9 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +9 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/validate-klados.d.ts +38 -0
- package/dist/validation/validate-klados.d.ts.map +1 -0
- package/dist/validation/validate-klados.js +139 -0
- package/dist/validation/validate-klados.js.map +1 -0
- package/dist/validation/validate-rhiza.d.ts +29 -0
- package/dist/validation/validate-rhiza.d.ts.map +1 -0
- package/dist/validation/validate-rhiza.js +382 -0
- package/dist/validation/validate-rhiza.js.map +1 -0
- package/dist/validation/validate-runtime.d.ts +28 -0
- package/dist/validation/validate-runtime.d.ts.map +1 -0
- package/dist/validation/validate-runtime.js +212 -0
- package/dist/validation/validate-runtime.js.map +1 -0
- package/dist/worker/errors.d.ts +77 -0
- package/dist/worker/errors.d.ts.map +1 -0
- package/dist/worker/errors.js +143 -0
- package/dist/worker/errors.js.map +1 -0
- package/dist/worker/index.d.ts +8 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +8 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/job.d.ts +150 -0
- package/dist/worker/job.d.ts.map +1 -0
- package/dist/worker/job.js +280 -0
- package/dist/worker/job.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KladosJob - High-level klados job management
|
|
3
|
+
*
|
|
4
|
+
* Manages the full lifecycle of a klados job:
|
|
5
|
+
* - Logging (create, update, finalize)
|
|
6
|
+
* - Error handling (log + batch slot)
|
|
7
|
+
* - Workflow handoff (via interpretThen)
|
|
8
|
+
*
|
|
9
|
+
* Reduces ~80 lines of boilerplate to ~15 lines.
|
|
10
|
+
*/
|
|
11
|
+
import { ArkeClient } from '@arke-institute/sdk';
|
|
12
|
+
import { KladosLogger } from '../logging/logger';
|
|
13
|
+
import { writeKladosLog, updateLogWithHandoffs, updateLogStatus } from '../logging/writer';
|
|
14
|
+
import { interpretThen } from '../handoff/interpret';
|
|
15
|
+
import { generateId } from '../utils';
|
|
16
|
+
import { failKlados, toKladosError } from './errors';
|
|
17
|
+
/**
|
|
18
|
+
* KladosJob - Manages the full lifecycle of a klados job
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const job = KladosJob.accept(request, { agentId, agentVersion, authToken });
|
|
23
|
+
*
|
|
24
|
+
* ctx.waitUntil(job.run(async () => {
|
|
25
|
+
* job.log.info('Processing...');
|
|
26
|
+
* const outputs = await doWork();
|
|
27
|
+
* return outputs; // Job handles handoff + finalization
|
|
28
|
+
* }));
|
|
29
|
+
*
|
|
30
|
+
* return Response.json(job.acceptResponse);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class KladosJob {
|
|
34
|
+
/** The Arke client */
|
|
35
|
+
client;
|
|
36
|
+
/** Logger for this job */
|
|
37
|
+
log;
|
|
38
|
+
/** The original request */
|
|
39
|
+
request;
|
|
40
|
+
/** The acceptance response to return to caller */
|
|
41
|
+
acceptResponse;
|
|
42
|
+
/** Job configuration */
|
|
43
|
+
config;
|
|
44
|
+
/** Generated log ID */
|
|
45
|
+
logId;
|
|
46
|
+
// Internal state
|
|
47
|
+
state = 'accepted';
|
|
48
|
+
logFileId = null;
|
|
49
|
+
flow = null;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new KladosJob (private - use static methods)
|
|
52
|
+
*/
|
|
53
|
+
constructor(request, config) {
|
|
54
|
+
this.request = request;
|
|
55
|
+
this.config = config;
|
|
56
|
+
this.log = new KladosLogger();
|
|
57
|
+
this.logId = `log_${generateId()}`;
|
|
58
|
+
// Create Arke client
|
|
59
|
+
this.client = new ArkeClient({
|
|
60
|
+
baseUrl: request.api_base,
|
|
61
|
+
authToken: config.authToken,
|
|
62
|
+
});
|
|
63
|
+
// Build acceptance response
|
|
64
|
+
this.acceptResponse = {
|
|
65
|
+
accepted: true,
|
|
66
|
+
job_id: request.job_id,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Accept a klados request and create a job
|
|
71
|
+
*
|
|
72
|
+
* This is the main factory method. Call this immediately when receiving
|
|
73
|
+
* a request, then process the job asynchronously.
|
|
74
|
+
*
|
|
75
|
+
* @param request - The incoming KladosRequest
|
|
76
|
+
* @param config - Job configuration
|
|
77
|
+
* @returns A new KladosJob instance
|
|
78
|
+
*/
|
|
79
|
+
static accept(request, config) {
|
|
80
|
+
return new KladosJob(request, config);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the batch context (if in a scatter/gather)
|
|
84
|
+
*/
|
|
85
|
+
get batchContext() {
|
|
86
|
+
return this.request.rhiza?.batch;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Check if this job is part of a workflow
|
|
90
|
+
*/
|
|
91
|
+
get isWorkflow() {
|
|
92
|
+
return !!this.request.rhiza;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Run the job with automatic lifecycle management
|
|
96
|
+
*
|
|
97
|
+
* This is the recommended way to process a klados job. It handles:
|
|
98
|
+
* - Writing the initial log entry
|
|
99
|
+
* - Catching and recording errors
|
|
100
|
+
* - Executing workflow handoffs
|
|
101
|
+
* - Finalizing the log
|
|
102
|
+
*
|
|
103
|
+
* @param fn - The processing function that returns output entity IDs
|
|
104
|
+
* @returns The job result
|
|
105
|
+
*/
|
|
106
|
+
async run(fn, options) {
|
|
107
|
+
// Start the job (write initial log)
|
|
108
|
+
await this.start();
|
|
109
|
+
try {
|
|
110
|
+
// Execute the processing function
|
|
111
|
+
const outputs = await fn();
|
|
112
|
+
// Complete the job (handoff + finalize)
|
|
113
|
+
return await this.complete(outputs, options?.outputProperties);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
// Handle failure
|
|
117
|
+
await this.fail(error);
|
|
118
|
+
throw error; // Re-throw so caller knows it failed
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Start the job (write initial log entry)
|
|
123
|
+
*
|
|
124
|
+
* For advanced use cases where you need more control over the lifecycle.
|
|
125
|
+
* Most users should use `run()` instead.
|
|
126
|
+
*/
|
|
127
|
+
async start() {
|
|
128
|
+
if (this.state !== 'accepted') {
|
|
129
|
+
throw new Error(`Cannot start job in state: ${this.state}`);
|
|
130
|
+
}
|
|
131
|
+
// Build log entry
|
|
132
|
+
const logEntry = {
|
|
133
|
+
id: this.logId,
|
|
134
|
+
type: 'klados_log',
|
|
135
|
+
klados_id: this.config.agentId,
|
|
136
|
+
rhiza_id: this.request.rhiza?.id,
|
|
137
|
+
job_id: this.request.job_id,
|
|
138
|
+
started_at: new Date().toISOString(),
|
|
139
|
+
status: 'running',
|
|
140
|
+
received: {
|
|
141
|
+
target: this.request.target,
|
|
142
|
+
from_logs: this.request.rhiza?.parent_logs,
|
|
143
|
+
batch: this.request.rhiza?.batch,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
// Write initial log entry
|
|
147
|
+
const { fileId } = await writeKladosLog({
|
|
148
|
+
client: this.client,
|
|
149
|
+
jobCollectionId: this.request.job_collection,
|
|
150
|
+
entry: logEntry,
|
|
151
|
+
messages: this.log.getMessages(),
|
|
152
|
+
agentId: this.config.agentId,
|
|
153
|
+
agentVersion: this.config.agentVersion,
|
|
154
|
+
});
|
|
155
|
+
this.logFileId = fileId;
|
|
156
|
+
this.state = 'started';
|
|
157
|
+
// Fetch rhiza flow if in a workflow
|
|
158
|
+
if (this.request.rhiza) {
|
|
159
|
+
await this.fetchFlow();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Complete the job with outputs
|
|
164
|
+
*
|
|
165
|
+
* For advanced use cases. Handles workflow handoff and log finalization.
|
|
166
|
+
*
|
|
167
|
+
* @param outputs - Output entity IDs produced by this job
|
|
168
|
+
* @param outputProperties - Properties of the primary output (for routing)
|
|
169
|
+
*/
|
|
170
|
+
async complete(outputs, outputProperties) {
|
|
171
|
+
if (this.state !== 'started') {
|
|
172
|
+
throw new Error(`Cannot complete job in state: ${this.state}`);
|
|
173
|
+
}
|
|
174
|
+
if (!this.logFileId) {
|
|
175
|
+
throw new Error('Job not started - logFileId is null');
|
|
176
|
+
}
|
|
177
|
+
let handoffResult;
|
|
178
|
+
const handoffs = [];
|
|
179
|
+
// Execute workflow handoff if in a workflow
|
|
180
|
+
if (this.request.rhiza && this.flow) {
|
|
181
|
+
const myStep = this.flow[this.config.agentId];
|
|
182
|
+
if (myStep?.then) {
|
|
183
|
+
handoffResult = await interpretThen(myStep.then, {
|
|
184
|
+
client: this.client,
|
|
185
|
+
rhizaId: this.request.rhiza.id,
|
|
186
|
+
kladosId: this.config.agentId,
|
|
187
|
+
jobId: this.request.job_id,
|
|
188
|
+
jobCollectionId: this.request.job_collection,
|
|
189
|
+
flow: this.flow,
|
|
190
|
+
outputs,
|
|
191
|
+
outputProperties,
|
|
192
|
+
fromLogId: this.logId,
|
|
193
|
+
path: this.request.rhiza.path,
|
|
194
|
+
apiBase: this.request.api_base,
|
|
195
|
+
network: this.request.network,
|
|
196
|
+
batchContext: this.request.rhiza.batch,
|
|
197
|
+
});
|
|
198
|
+
if (handoffResult.handoffRecord) {
|
|
199
|
+
handoffs.push(handoffResult.handoffRecord);
|
|
200
|
+
}
|
|
201
|
+
this.log.info(`Handoff: ${handoffResult.action}`, {
|
|
202
|
+
target: handoffResult.target,
|
|
203
|
+
targetType: handoffResult.targetType,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Update log with handoffs
|
|
208
|
+
if (handoffs.length > 0) {
|
|
209
|
+
await updateLogWithHandoffs(this.client, this.logFileId, handoffs);
|
|
210
|
+
}
|
|
211
|
+
// Mark log as done
|
|
212
|
+
await updateLogStatus(this.client, this.logFileId, 'done');
|
|
213
|
+
this.state = 'completed';
|
|
214
|
+
this.log.success('Job completed');
|
|
215
|
+
return {
|
|
216
|
+
handoff: handoffResult,
|
|
217
|
+
outputs,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Mark the job as failed
|
|
222
|
+
*
|
|
223
|
+
* Handles both log status update AND batch slot error (if applicable).
|
|
224
|
+
*
|
|
225
|
+
* @param error - The error that caused the failure
|
|
226
|
+
*/
|
|
227
|
+
async fail(error) {
|
|
228
|
+
if (this.state === 'completed' || this.state === 'failed') {
|
|
229
|
+
return; // Already finalized
|
|
230
|
+
}
|
|
231
|
+
const kladosError = toKladosError(error);
|
|
232
|
+
this.log.error('Job failed', { code: kladosError.code, message: kladosError.message });
|
|
233
|
+
// If we haven't started yet, we can't update the log
|
|
234
|
+
if (!this.logFileId) {
|
|
235
|
+
this.state = 'failed';
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// Use failKlados to handle both log and batch slot
|
|
239
|
+
await failKlados(this.client, {
|
|
240
|
+
logFileId: this.logFileId,
|
|
241
|
+
batchContext: this.request.rhiza?.batch,
|
|
242
|
+
error: kladosError,
|
|
243
|
+
});
|
|
244
|
+
this.state = 'failed';
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Fetch the target entity
|
|
248
|
+
*
|
|
249
|
+
* Convenience method to fetch the entity being processed.
|
|
250
|
+
*/
|
|
251
|
+
async fetchTarget() {
|
|
252
|
+
const { data, error } = await this.client.api.GET('/entities/{id}', {
|
|
253
|
+
params: { path: { id: this.request.target } },
|
|
254
|
+
});
|
|
255
|
+
if (error || !data) {
|
|
256
|
+
throw new Error(`Failed to fetch target entity: ${this.request.target}`);
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
id: data.id,
|
|
260
|
+
type: data.type,
|
|
261
|
+
properties: data.properties,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Fetch the rhiza flow definition
|
|
266
|
+
*/
|
|
267
|
+
async fetchFlow() {
|
|
268
|
+
if (!this.request.rhiza) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const { data, error } = await this.client.api.GET('/entities/{id}', {
|
|
272
|
+
params: { path: { id: this.request.rhiza.id } },
|
|
273
|
+
});
|
|
274
|
+
if (error || !data) {
|
|
275
|
+
throw new Error(`Failed to fetch rhiza: ${this.request.rhiza.id}`);
|
|
276
|
+
}
|
|
277
|
+
this.flow = data.properties.flow;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=job.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job.js","sourceRoot":"","sources":["../../src/worker/job.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AASjD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC3F,OAAO,EAAE,aAAa,EAAwB,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAsCrD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,SAAS;IACpB,sBAAsB;IACb,MAAM,CAAa;IAE5B,0BAA0B;IACjB,GAAG,CAAe;IAE3B,2BAA2B;IAClB,OAAO,CAAgB;IAEhC,kDAAkD;IACzC,cAAc,CAAiB;IAExC,wBAAwB;IACf,MAAM,CAAkB;IAEjC,uBAAuB;IACd,KAAK,CAAS;IAEvB,iBAAiB;IACT,KAAK,GAAa,UAAU,CAAC;IAC7B,SAAS,GAAkB,IAAI,CAAC;IAChC,IAAI,GAAoC,IAAI,CAAC;IAErD;;OAEG;IACH,YACE,OAAsB,EACtB,MAAuB;QAEvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;QAC9B,IAAI,CAAC,KA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;SAChD,CAAC,CAAC;QAEH,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QACrE,CAAC;QAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,IAAgC,CAAC;IAC/D,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arke-institute/rhiza",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Workflow protocol for Arke - cascading handoff pattern for distributed actions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"dev": "tsc --watch",
|
|
20
|
+
"test": "vitest",
|
|
21
|
+
"test:run": "vitest run",
|
|
22
|
+
"type-check": "tsc --noEmit",
|
|
23
|
+
"clean": "rm -rf dist"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@arke-institute/sdk": "^2.9.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"@arke-institute/sdk": {
|
|
30
|
+
"optional": true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@arke-institute/sdk": "^2.9.0",
|
|
35
|
+
"@types/node": "^22.0.0",
|
|
36
|
+
"typescript": "^5.7.2",
|
|
37
|
+
"vitest": "^2.0.0"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"arke",
|
|
41
|
+
"workflow",
|
|
42
|
+
"orchestration",
|
|
43
|
+
"distributed",
|
|
44
|
+
"protocol"
|
|
45
|
+
],
|
|
46
|
+
"author": "Arke Institute",
|
|
47
|
+
"license": "MIT"
|
|
48
|
+
}
|