@arbidocs/sdk 0.4.74 → 0.4.76

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/dist/browser.cjs CHANGED
@@ -6066,12 +6066,267 @@ __export(work_exports, {
6066
6066
  actOnStep: () => actOnStep,
6067
6067
  createWork: () => createWork,
6068
6068
  getActivity: () => getActivity,
6069
+ getConversation: () => getConversation,
6069
6070
  getPendingAuth: () => getPendingAuth,
6071
+ getRunProgress: () => getRunProgress,
6072
+ getStepAnswers: () => getStepAnswers,
6070
6073
  notifyConversation: () => notifyConversation,
6071
6074
  planWork: () => planWork,
6072
6075
  runConversation: () => runConversation,
6073
6076
  updateConversation: () => updateConversation
6074
6077
  });
6078
+
6079
+ // src/workflow-steps.ts
6080
+ var workflow_steps_exports = {};
6081
+ __export(workflow_steps_exports, {
6082
+ PERSON_STEP_KINDS: () => PERSON_STEP_KINDS,
6083
+ PlanStepsError: () => PlanStepsError,
6084
+ STEP_ID_PATTERN: () => STEP_ID_PATTERN,
6085
+ STEP_KINDS: () => STEP_KINDS,
6086
+ isPersonStep: () => isPersonStep,
6087
+ missingRequiredFields: () => missingRequiredFields,
6088
+ parseInputPairs: () => parseInputPairs,
6089
+ parsePlanSteps: () => parsePlanSteps,
6090
+ runSteps: () => runSteps,
6091
+ runWaitingOn: () => runWaitingOn,
6092
+ stepDependencies: () => stepDependencies,
6093
+ stepFields: () => stepFields,
6094
+ stepPhase: () => stepPhase,
6095
+ stepStatesFromActivity: () => stepStatesFromActivity,
6096
+ stepsPlan: () => stepsPlan
6097
+ });
6098
+ var STEP_KINDS = ["form", "upload", "agent", "review"];
6099
+ var PERSON_STEP_KINDS = ["form", "upload", "review"];
6100
+ var STEP_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
6101
+ var STEP_EVENTS = {
6102
+ step_started: "running",
6103
+ step_done: "done",
6104
+ step_skipped: "skipped",
6105
+ step_failed: "failed"
6106
+ };
6107
+ var isFinished = (state) => state?.status === "done" || state?.status === "skipped";
6108
+ var isPersonStep = (step) => PERSON_STEP_KINDS.includes(step.kind);
6109
+ function stepDependencies(steps, id) {
6110
+ const i2 = steps.findIndex((s2) => s2.id === id);
6111
+ if (i2 < 0) return [];
6112
+ const after = steps[i2].after;
6113
+ if (after !== null && after !== void 0) return [...after];
6114
+ return i2 > 0 ? [steps[i2 - 1].id] : [];
6115
+ }
6116
+ function dependsOn(steps, a2, b2) {
6117
+ const seen = /* @__PURE__ */ new Set();
6118
+ const stack = stepDependencies(steps, a2);
6119
+ while (stack.length) {
6120
+ const id = stack.pop();
6121
+ if (id === b2) return true;
6122
+ if (seen.has(id)) continue;
6123
+ seen.add(id);
6124
+ stack.push(...stepDependencies(steps, id));
6125
+ }
6126
+ return false;
6127
+ }
6128
+ function stepStatesFromActivity(events) {
6129
+ const states = {};
6130
+ for (const e of events) {
6131
+ const status = STEP_EVENTS[e.kind];
6132
+ const step = e.detail?.step;
6133
+ if (!status || typeof step !== "string") continue;
6134
+ const responseId = typeof e.detail?.response_id === "string" ? e.detail.response_id : null;
6135
+ const error = typeof e.detail?.error === "string" ? e.detail.error : null;
6136
+ states[step] = { status, responseId: responseId ?? states[step]?.responseId ?? null, error };
6137
+ }
6138
+ return states;
6139
+ }
6140
+ function stepPhase(steps, states, id) {
6141
+ const state = states[id];
6142
+ if (state && state.status !== "pending") return state.status;
6143
+ return stepDependencies(steps, id).every((d2) => isFinished(states[d2])) ? "ready" : "pending";
6144
+ }
6145
+ function runSteps(steps, events) {
6146
+ const states = stepStatesFromActivity(events);
6147
+ return steps.map((s2) => ({
6148
+ id: s2.id,
6149
+ title: s2.title,
6150
+ kind: s2.kind,
6151
+ phase: stepPhase(steps, states, s2.id),
6152
+ after: stepDependencies(steps, s2.id),
6153
+ responseId: states[s2.id]?.responseId ?? null,
6154
+ error: states[s2.id]?.error ?? null
6155
+ }));
6156
+ }
6157
+ function runWaitingOn(run) {
6158
+ if (run.length > 0 && run.every((s2) => s2.phase === "done" || s2.phase === "skipped"))
6159
+ return "finished";
6160
+ if (run.some((s2) => s2.phase === "running" || s2.kind === "agent" && s2.phase === "ready"))
6161
+ return "agent";
6162
+ if (run.some((s2) => s2.kind !== "agent" && s2.phase === "ready")) return "person";
6163
+ return "stuck";
6164
+ }
6165
+ function stepFields(step) {
6166
+ if (step.kind !== "form") return [];
6167
+ const fields = step.ui?.fields;
6168
+ return Array.isArray(fields) ? fields : [];
6169
+ }
6170
+ var blank = (value) => value === void 0 || value === null || typeof value === "string" && value.trim() === "";
6171
+ function missingRequiredFields(step, inputs) {
6172
+ return stepFields(step).filter((f2) => f2.required && blank(inputs[f2.name])).map((f2) => f2.name);
6173
+ }
6174
+ function parseInputPairs(pairs, fields = []) {
6175
+ const types = new Map(fields.map((f2) => [f2.name, f2.type]));
6176
+ const out = {};
6177
+ for (const pair of pairs) {
6178
+ const eq = pair.indexOf("=");
6179
+ if (eq <= 0) throw new Error(`Invalid input "${pair}": use name=value`);
6180
+ const name = pair.slice(0, eq).trim();
6181
+ const raw = pair.slice(eq + 1);
6182
+ const type = types.get(name);
6183
+ if (type === "number") {
6184
+ const n2 = Number(raw);
6185
+ if (raw.trim() === "" || Number.isNaN(n2))
6186
+ throw new Error(`Input "${name}" must be a number, got "${raw}"`);
6187
+ out[name] = n2;
6188
+ } else if (type === "boolean") {
6189
+ if (!["true", "false"].includes(raw.toLowerCase()))
6190
+ throw new Error(`Input "${name}" must be true or false, got "${raw}"`);
6191
+ out[name] = raw.toLowerCase() === "true";
6192
+ } else out[name] = raw;
6193
+ }
6194
+ return out;
6195
+ }
6196
+ var STEP_KEYS = /* @__PURE__ */ new Set([
6197
+ "id",
6198
+ "title",
6199
+ "kind",
6200
+ "after",
6201
+ "skill",
6202
+ "prompt",
6203
+ "inputs",
6204
+ "branch",
6205
+ "requires_files",
6206
+ "ui"
6207
+ ]);
6208
+ var isRecord = (v2) => typeof v2 === "object" && v2 !== null && !Array.isArray(v2);
6209
+ var PlanStepsError = class extends Error {
6210
+ constructor(problems) {
6211
+ super(`Invalid workflow steps:
6212
+ ${problems.map((p2) => ` - ${p2}`).join("\n")}`);
6213
+ this.problems = problems;
6214
+ this.name = "PlanStepsError";
6215
+ }
6216
+ problems;
6217
+ };
6218
+ function checkStep(raw, at2, problems) {
6219
+ if (!isRecord(raw)) {
6220
+ problems.push(`${at2}: must be an object with id, title and kind`);
6221
+ return null;
6222
+ }
6223
+ const before = problems.length;
6224
+ for (const key of Object.keys(raw))
6225
+ if (!STEP_KEYS.has(key))
6226
+ problems.push(`${at2}.${key}: unknown field (allowed: ${[...STEP_KEYS].join(", ")})`);
6227
+ const { id, title, kind, after, skill, prompt, inputs, branch, ui } = raw;
6228
+ if (typeof id !== "string" || !STEP_ID_PATTERN.test(id))
6229
+ problems.push(
6230
+ `${at2}.id: must be lowercase letters, digits, "_" or "-", starting with a letter (got ${JSON.stringify(id)})`
6231
+ );
6232
+ if (typeof title !== "string" || !title.trim()) problems.push(`${at2}.title: required text`);
6233
+ if (typeof kind !== "string" || !STEP_KINDS.includes(kind))
6234
+ problems.push(
6235
+ `${at2}.kind: must be one of ${STEP_KINDS.join(", ")} (got ${JSON.stringify(kind)})`
6236
+ );
6237
+ if (after !== void 0 && after !== null) {
6238
+ if (!Array.isArray(after) || after.some((d2) => typeof d2 !== "string"))
6239
+ problems.push(`${at2}.after: must be a list of step ids`);
6240
+ }
6241
+ for (const [key, value] of [
6242
+ ["skill", skill],
6243
+ ["prompt", prompt]
6244
+ ])
6245
+ if (value !== void 0 && value !== null && typeof value !== "string")
6246
+ problems.push(`${at2}.${key}: must be text`);
6247
+ if (inputs !== void 0 && inputs !== null) {
6248
+ if (!isRecord(inputs) || Object.values(inputs).some((v2) => typeof v2 !== "string"))
6249
+ problems.push(
6250
+ `${at2}.inputs: must map each skill input to a text template, e.g. {"q": "{{topic}}"}`
6251
+ );
6252
+ }
6253
+ if (branch !== void 0 && typeof branch !== "boolean")
6254
+ problems.push(`${at2}.branch: true or false`);
6255
+ if (ui !== void 0 && ui !== null && !isRecord(ui)) problems.push(`${at2}.ui: must be an object`);
6256
+ if (kind === "agent") {
6257
+ const hasSkill = typeof skill === "string" && skill.trim() !== "";
6258
+ const hasPrompt = typeof prompt === "string" && prompt.trim() !== "";
6259
+ if (!hasSkill && !hasPrompt) problems.push(`${at2}: an agent step needs a prompt or a skill`);
6260
+ if (hasSkill && hasPrompt)
6261
+ problems.push(`${at2}: an agent step runs a prompt or a skill, not both`);
6262
+ } else if (typeof kind === "string" && STEP_KINDS.includes(kind)) {
6263
+ for (const [key, value] of [
6264
+ ["skill", skill],
6265
+ ["prompt", prompt],
6266
+ ["inputs", inputs]
6267
+ ])
6268
+ if (value !== void 0 && value !== null)
6269
+ problems.push(`${at2}.${key}: only agent steps have a ${key} (this is a ${kind} step)`);
6270
+ if (branch) problems.push(`${at2}.branch: only agent steps can branch`);
6271
+ }
6272
+ if (kind === "form" && isRecord(ui) && ui.fields !== void 0) {
6273
+ if (!Array.isArray(ui.fields)) problems.push(`${at2}.ui.fields: must be a list of fields`);
6274
+ else
6275
+ ui.fields.forEach((f2, j2) => {
6276
+ if (!isRecord(f2) || typeof f2.name !== "string" || !/^[a-z][a-z0-9_]*$/i.test(f2.name))
6277
+ problems.push(`${at2}.ui.fields[${j2}].name: a field needs a name like "client_name"`);
6278
+ });
6279
+ }
6280
+ if (problems.length > before) return null;
6281
+ const fields = kind === "form" && isRecord(ui) && Array.isArray(ui.fields) ? ui.fields.map((f2) => ({
6282
+ ...f2,
6283
+ type: typeof f2.type === "string" ? f2.type : "text",
6284
+ label: typeof f2.label === "string" ? f2.label : f2.name
6285
+ })) : void 0;
6286
+ return {
6287
+ id,
6288
+ title: title.trim(),
6289
+ kind,
6290
+ after: after ?? null,
6291
+ skill: skill ?? null,
6292
+ prompt: prompt ?? null,
6293
+ inputs: inputs ?? null,
6294
+ branch: !!branch,
6295
+ requires_files: !!raw.requires_files,
6296
+ ui: isRecord(ui) ? { ...ui, ...fields ? { fields } : {} } : null
6297
+ };
6298
+ }
6299
+ function parsePlanSteps(value) {
6300
+ const list = isRecord(value) ? value.steps : value;
6301
+ if (!Array.isArray(list))
6302
+ throw new PlanStepsError(['expected a list of steps, or an object with a "steps" list']);
6303
+ if (list.length === 0) throw new PlanStepsError(["a workflow needs at least one step"]);
6304
+ const problems = [];
6305
+ const steps = list.map((raw, i2) => checkStep(raw, `steps[${i2}]`, problems));
6306
+ if (problems.length) throw new PlanStepsError(problems);
6307
+ const valid = steps;
6308
+ const ids = /* @__PURE__ */ new Set();
6309
+ valid.forEach((s2, i2) => {
6310
+ if (ids.has(s2.id)) problems.push(`steps[${i2}].id: "${s2.id}" is used twice`);
6311
+ ids.add(s2.id);
6312
+ });
6313
+ valid.forEach((s2, i2) => {
6314
+ for (const d2 of stepDependencies(valid, s2.id)) {
6315
+ if (!ids.has(d2)) problems.push(`steps[${i2}].after: no step "${d2}"`);
6316
+ else if (d2 === s2.id || dependsOn(valid, d2, s2.id))
6317
+ problems.push(`steps[${i2}].after: "${s2.id}" and "${d2}" wait for each other`);
6318
+ else if (valid.findIndex((x2) => x2.id === d2) > i2)
6319
+ problems.push(`steps[${i2}].after: "${d2}" must come before "${s2.id}" in the list`);
6320
+ }
6321
+ });
6322
+ if (problems.length) throw new PlanStepsError(problems);
6323
+ return valid;
6324
+ }
6325
+ function stepsPlan(steps) {
6326
+ return { goal: "", criteria: [], max_passes: 3, documents: [], steps };
6327
+ }
6328
+
6329
+ // src/operations/work.ts
6075
6330
  var byId = (conversationId) => ({
6076
6331
  params: { path: { conversation_ext_id: conversationId } }
6077
6332
  });
@@ -6135,6 +6390,39 @@ async function planWork(arbi, body) {
6135
6390
  "Failed to draft a plan"
6136
6391
  );
6137
6392
  }
6393
+ async function getConversation(arbi, conversationId) {
6394
+ const all = requireData(
6395
+ await arbi.fetch.GET("/v1/conversation/list"),
6396
+ "Failed to fetch conversations"
6397
+ );
6398
+ const found = all.find((c2) => c2.external_id === conversationId);
6399
+ if (!found) throw new Error(`No conversation ${conversationId} in this workspace`);
6400
+ return found;
6401
+ }
6402
+ async function getRunProgress(arbi, conversationId) {
6403
+ const [conversation, activity] = await Promise.all([
6404
+ getConversation(arbi, conversationId),
6405
+ getActivity(arbi, conversationId)
6406
+ ]);
6407
+ const plan = conversation.plan?.steps ?? [];
6408
+ if (!plan.length)
6409
+ throw new Error(`${conversationId} is not a stepped workflow run (its plan has no steps)`);
6410
+ const steps = runSteps(plan, activity);
6411
+ return { conversation, steps, waitingOn: runWaitingOn(steps) };
6412
+ }
6413
+ async function getStepAnswers(arbi, conversationId, responseIds) {
6414
+ if (!responseIds.length) return {};
6415
+ const data = requireData(
6416
+ await arbi.fetch.GET("/v1/conversation/{conversation_ext_id}/threads", byId(conversationId)),
6417
+ "Failed to fetch the thread"
6418
+ );
6419
+ const wanted = new Set(responseIds);
6420
+ const out = {};
6421
+ for (const thread of data.threads ?? [])
6422
+ for (const m2 of thread.history ?? [])
6423
+ if (wanted.has(m2.external_id) && typeof m2.content === "string") out[m2.external_id] = m2.content;
6424
+ return out;
6425
+ }
6138
6426
 
6139
6427
  // src/operations/workflows.ts
6140
6428
  var workflows_exports = {};
@@ -7278,6 +7566,7 @@ function bytesToBase642(bytes) {
7278
7566
  exports.Arbi = Arbi;
7279
7567
  exports.ArbiApiError = ArbiApiError;
7280
7568
  exports.ArbiError = ArbiError;
7569
+ exports.PlanStepsError = PlanStepsError;
7281
7570
  exports.WebSocketAuthError = WebSocketAuthError;
7282
7571
  exports.account = account_exports;
7283
7572
  exports.agentconfig = agentconfig_exports;
@@ -7332,6 +7621,7 @@ exports.stripCitationMarkdown = stripCitationMarkdown;
7332
7621
  exports.summarizeCitations = summarizeCitations;
7333
7622
  exports.tags = tags_exports;
7334
7623
  exports.work = work_exports;
7624
+ exports.workflowSteps = workflow_steps_exports;
7335
7625
  exports.workflows = workflows_exports;
7336
7626
  exports.workspaces = workspaces_exports;
7337
7627
  //# sourceMappingURL=browser.cjs.map