@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/index.cjs CHANGED
@@ -6630,12 +6630,267 @@ __export(work_exports, {
6630
6630
  actOnStep: () => actOnStep,
6631
6631
  createWork: () => createWork,
6632
6632
  getActivity: () => getActivity,
6633
+ getConversation: () => getConversation,
6633
6634
  getPendingAuth: () => getPendingAuth,
6635
+ getRunProgress: () => getRunProgress,
6636
+ getStepAnswers: () => getStepAnswers,
6634
6637
  notifyConversation: () => notifyConversation,
6635
6638
  planWork: () => planWork,
6636
6639
  runConversation: () => runConversation,
6637
6640
  updateConversation: () => updateConversation
6638
6641
  });
6642
+
6643
+ // src/workflow-steps.ts
6644
+ var workflow_steps_exports = {};
6645
+ __export(workflow_steps_exports, {
6646
+ PERSON_STEP_KINDS: () => PERSON_STEP_KINDS,
6647
+ PlanStepsError: () => PlanStepsError,
6648
+ STEP_ID_PATTERN: () => STEP_ID_PATTERN,
6649
+ STEP_KINDS: () => STEP_KINDS,
6650
+ isPersonStep: () => isPersonStep,
6651
+ missingRequiredFields: () => missingRequiredFields,
6652
+ parseInputPairs: () => parseInputPairs,
6653
+ parsePlanSteps: () => parsePlanSteps,
6654
+ runSteps: () => runSteps,
6655
+ runWaitingOn: () => runWaitingOn,
6656
+ stepDependencies: () => stepDependencies,
6657
+ stepFields: () => stepFields,
6658
+ stepPhase: () => stepPhase,
6659
+ stepStatesFromActivity: () => stepStatesFromActivity,
6660
+ stepsPlan: () => stepsPlan
6661
+ });
6662
+ var STEP_KINDS = ["form", "upload", "agent", "review"];
6663
+ var PERSON_STEP_KINDS = ["form", "upload", "review"];
6664
+ var STEP_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
6665
+ var STEP_EVENTS = {
6666
+ step_started: "running",
6667
+ step_done: "done",
6668
+ step_skipped: "skipped",
6669
+ step_failed: "failed"
6670
+ };
6671
+ var isFinished = (state) => state?.status === "done" || state?.status === "skipped";
6672
+ var isPersonStep = (step) => PERSON_STEP_KINDS.includes(step.kind);
6673
+ function stepDependencies(steps, id) {
6674
+ const i2 = steps.findIndex((s2) => s2.id === id);
6675
+ if (i2 < 0) return [];
6676
+ const after = steps[i2].after;
6677
+ if (after !== null && after !== void 0) return [...after];
6678
+ return i2 > 0 ? [steps[i2 - 1].id] : [];
6679
+ }
6680
+ function dependsOn(steps, a2, b2) {
6681
+ const seen = /* @__PURE__ */ new Set();
6682
+ const stack = stepDependencies(steps, a2);
6683
+ while (stack.length) {
6684
+ const id = stack.pop();
6685
+ if (id === b2) return true;
6686
+ if (seen.has(id)) continue;
6687
+ seen.add(id);
6688
+ stack.push(...stepDependencies(steps, id));
6689
+ }
6690
+ return false;
6691
+ }
6692
+ function stepStatesFromActivity(events) {
6693
+ const states = {};
6694
+ for (const e of events) {
6695
+ const status = STEP_EVENTS[e.kind];
6696
+ const step = e.detail?.step;
6697
+ if (!status || typeof step !== "string") continue;
6698
+ const responseId = typeof e.detail?.response_id === "string" ? e.detail.response_id : null;
6699
+ const error = typeof e.detail?.error === "string" ? e.detail.error : null;
6700
+ states[step] = { status, responseId: responseId ?? states[step]?.responseId ?? null, error };
6701
+ }
6702
+ return states;
6703
+ }
6704
+ function stepPhase(steps, states, id) {
6705
+ const state = states[id];
6706
+ if (state && state.status !== "pending") return state.status;
6707
+ return stepDependencies(steps, id).every((d2) => isFinished(states[d2])) ? "ready" : "pending";
6708
+ }
6709
+ function runSteps(steps, events) {
6710
+ const states = stepStatesFromActivity(events);
6711
+ return steps.map((s2) => ({
6712
+ id: s2.id,
6713
+ title: s2.title,
6714
+ kind: s2.kind,
6715
+ phase: stepPhase(steps, states, s2.id),
6716
+ after: stepDependencies(steps, s2.id),
6717
+ responseId: states[s2.id]?.responseId ?? null,
6718
+ error: states[s2.id]?.error ?? null
6719
+ }));
6720
+ }
6721
+ function runWaitingOn(run) {
6722
+ if (run.length > 0 && run.every((s2) => s2.phase === "done" || s2.phase === "skipped"))
6723
+ return "finished";
6724
+ if (run.some((s2) => s2.phase === "running" || s2.kind === "agent" && s2.phase === "ready"))
6725
+ return "agent";
6726
+ if (run.some((s2) => s2.kind !== "agent" && s2.phase === "ready")) return "person";
6727
+ return "stuck";
6728
+ }
6729
+ function stepFields(step) {
6730
+ if (step.kind !== "form") return [];
6731
+ const fields = step.ui?.fields;
6732
+ return Array.isArray(fields) ? fields : [];
6733
+ }
6734
+ var blank = (value) => value === void 0 || value === null || typeof value === "string" && value.trim() === "";
6735
+ function missingRequiredFields(step, inputs) {
6736
+ return stepFields(step).filter((f2) => f2.required && blank(inputs[f2.name])).map((f2) => f2.name);
6737
+ }
6738
+ function parseInputPairs(pairs, fields = []) {
6739
+ const types = new Map(fields.map((f2) => [f2.name, f2.type]));
6740
+ const out = {};
6741
+ for (const pair of pairs) {
6742
+ const eq = pair.indexOf("=");
6743
+ if (eq <= 0) throw new Error(`Invalid input "${pair}": use name=value`);
6744
+ const name = pair.slice(0, eq).trim();
6745
+ const raw = pair.slice(eq + 1);
6746
+ const type = types.get(name);
6747
+ if (type === "number") {
6748
+ const n2 = Number(raw);
6749
+ if (raw.trim() === "" || Number.isNaN(n2))
6750
+ throw new Error(`Input "${name}" must be a number, got "${raw}"`);
6751
+ out[name] = n2;
6752
+ } else if (type === "boolean") {
6753
+ if (!["true", "false"].includes(raw.toLowerCase()))
6754
+ throw new Error(`Input "${name}" must be true or false, got "${raw}"`);
6755
+ out[name] = raw.toLowerCase() === "true";
6756
+ } else out[name] = raw;
6757
+ }
6758
+ return out;
6759
+ }
6760
+ var STEP_KEYS = /* @__PURE__ */ new Set([
6761
+ "id",
6762
+ "title",
6763
+ "kind",
6764
+ "after",
6765
+ "skill",
6766
+ "prompt",
6767
+ "inputs",
6768
+ "branch",
6769
+ "requires_files",
6770
+ "ui"
6771
+ ]);
6772
+ var isRecord = (v2) => typeof v2 === "object" && v2 !== null && !Array.isArray(v2);
6773
+ var PlanStepsError = class extends Error {
6774
+ constructor(problems) {
6775
+ super(`Invalid workflow steps:
6776
+ ${problems.map((p2) => ` - ${p2}`).join("\n")}`);
6777
+ this.problems = problems;
6778
+ this.name = "PlanStepsError";
6779
+ }
6780
+ problems;
6781
+ };
6782
+ function checkStep(raw, at2, problems) {
6783
+ if (!isRecord(raw)) {
6784
+ problems.push(`${at2}: must be an object with id, title and kind`);
6785
+ return null;
6786
+ }
6787
+ const before = problems.length;
6788
+ for (const key of Object.keys(raw))
6789
+ if (!STEP_KEYS.has(key))
6790
+ problems.push(`${at2}.${key}: unknown field (allowed: ${[...STEP_KEYS].join(", ")})`);
6791
+ const { id, title, kind, after, skill, prompt, inputs, branch, ui } = raw;
6792
+ if (typeof id !== "string" || !STEP_ID_PATTERN.test(id))
6793
+ problems.push(
6794
+ `${at2}.id: must be lowercase letters, digits, "_" or "-", starting with a letter (got ${JSON.stringify(id)})`
6795
+ );
6796
+ if (typeof title !== "string" || !title.trim()) problems.push(`${at2}.title: required text`);
6797
+ if (typeof kind !== "string" || !STEP_KINDS.includes(kind))
6798
+ problems.push(
6799
+ `${at2}.kind: must be one of ${STEP_KINDS.join(", ")} (got ${JSON.stringify(kind)})`
6800
+ );
6801
+ if (after !== void 0 && after !== null) {
6802
+ if (!Array.isArray(after) || after.some((d2) => typeof d2 !== "string"))
6803
+ problems.push(`${at2}.after: must be a list of step ids`);
6804
+ }
6805
+ for (const [key, value] of [
6806
+ ["skill", skill],
6807
+ ["prompt", prompt]
6808
+ ])
6809
+ if (value !== void 0 && value !== null && typeof value !== "string")
6810
+ problems.push(`${at2}.${key}: must be text`);
6811
+ if (inputs !== void 0 && inputs !== null) {
6812
+ if (!isRecord(inputs) || Object.values(inputs).some((v2) => typeof v2 !== "string"))
6813
+ problems.push(
6814
+ `${at2}.inputs: must map each skill input to a text template, e.g. {"q": "{{topic}}"}`
6815
+ );
6816
+ }
6817
+ if (branch !== void 0 && typeof branch !== "boolean")
6818
+ problems.push(`${at2}.branch: true or false`);
6819
+ if (ui !== void 0 && ui !== null && !isRecord(ui)) problems.push(`${at2}.ui: must be an object`);
6820
+ if (kind === "agent") {
6821
+ const hasSkill = typeof skill === "string" && skill.trim() !== "";
6822
+ const hasPrompt = typeof prompt === "string" && prompt.trim() !== "";
6823
+ if (!hasSkill && !hasPrompt) problems.push(`${at2}: an agent step needs a prompt or a skill`);
6824
+ if (hasSkill && hasPrompt)
6825
+ problems.push(`${at2}: an agent step runs a prompt or a skill, not both`);
6826
+ } else if (typeof kind === "string" && STEP_KINDS.includes(kind)) {
6827
+ for (const [key, value] of [
6828
+ ["skill", skill],
6829
+ ["prompt", prompt],
6830
+ ["inputs", inputs]
6831
+ ])
6832
+ if (value !== void 0 && value !== null)
6833
+ problems.push(`${at2}.${key}: only agent steps have a ${key} (this is a ${kind} step)`);
6834
+ if (branch) problems.push(`${at2}.branch: only agent steps can branch`);
6835
+ }
6836
+ if (kind === "form" && isRecord(ui) && ui.fields !== void 0) {
6837
+ if (!Array.isArray(ui.fields)) problems.push(`${at2}.ui.fields: must be a list of fields`);
6838
+ else
6839
+ ui.fields.forEach((f2, j2) => {
6840
+ if (!isRecord(f2) || typeof f2.name !== "string" || !/^[a-z][a-z0-9_]*$/i.test(f2.name))
6841
+ problems.push(`${at2}.ui.fields[${j2}].name: a field needs a name like "client_name"`);
6842
+ });
6843
+ }
6844
+ if (problems.length > before) return null;
6845
+ const fields = kind === "form" && isRecord(ui) && Array.isArray(ui.fields) ? ui.fields.map((f2) => ({
6846
+ ...f2,
6847
+ type: typeof f2.type === "string" ? f2.type : "text",
6848
+ label: typeof f2.label === "string" ? f2.label : f2.name
6849
+ })) : void 0;
6850
+ return {
6851
+ id,
6852
+ title: title.trim(),
6853
+ kind,
6854
+ after: after ?? null,
6855
+ skill: skill ?? null,
6856
+ prompt: prompt ?? null,
6857
+ inputs: inputs ?? null,
6858
+ branch: !!branch,
6859
+ requires_files: !!raw.requires_files,
6860
+ ui: isRecord(ui) ? { ...ui, ...fields ? { fields } : {} } : null
6861
+ };
6862
+ }
6863
+ function parsePlanSteps(value) {
6864
+ const list = isRecord(value) ? value.steps : value;
6865
+ if (!Array.isArray(list))
6866
+ throw new PlanStepsError(['expected a list of steps, or an object with a "steps" list']);
6867
+ if (list.length === 0) throw new PlanStepsError(["a workflow needs at least one step"]);
6868
+ const problems = [];
6869
+ const steps = list.map((raw, i2) => checkStep(raw, `steps[${i2}]`, problems));
6870
+ if (problems.length) throw new PlanStepsError(problems);
6871
+ const valid = steps;
6872
+ const ids = /* @__PURE__ */ new Set();
6873
+ valid.forEach((s2, i2) => {
6874
+ if (ids.has(s2.id)) problems.push(`steps[${i2}].id: "${s2.id}" is used twice`);
6875
+ ids.add(s2.id);
6876
+ });
6877
+ valid.forEach((s2, i2) => {
6878
+ for (const d2 of stepDependencies(valid, s2.id)) {
6879
+ if (!ids.has(d2)) problems.push(`steps[${i2}].after: no step "${d2}"`);
6880
+ else if (d2 === s2.id || dependsOn(valid, d2, s2.id))
6881
+ problems.push(`steps[${i2}].after: "${s2.id}" and "${d2}" wait for each other`);
6882
+ else if (valid.findIndex((x2) => x2.id === d2) > i2)
6883
+ problems.push(`steps[${i2}].after: "${d2}" must come before "${s2.id}" in the list`);
6884
+ }
6885
+ });
6886
+ if (problems.length) throw new PlanStepsError(problems);
6887
+ return valid;
6888
+ }
6889
+ function stepsPlan(steps) {
6890
+ return { goal: "", criteria: [], max_passes: 3, documents: [], steps };
6891
+ }
6892
+
6893
+ // src/operations/work.ts
6639
6894
  var byId = (conversationId) => ({
6640
6895
  params: { path: { conversation_ext_id: conversationId } }
6641
6896
  });
@@ -6699,6 +6954,39 @@ async function planWork(arbi, body) {
6699
6954
  "Failed to draft a plan"
6700
6955
  );
6701
6956
  }
6957
+ async function getConversation(arbi, conversationId) {
6958
+ const all = requireData(
6959
+ await arbi.fetch.GET("/v1/conversation/list"),
6960
+ "Failed to fetch conversations"
6961
+ );
6962
+ const found = all.find((c2) => c2.external_id === conversationId);
6963
+ if (!found) throw new Error(`No conversation ${conversationId} in this workspace`);
6964
+ return found;
6965
+ }
6966
+ async function getRunProgress(arbi, conversationId) {
6967
+ const [conversation, activity] = await Promise.all([
6968
+ getConversation(arbi, conversationId),
6969
+ getActivity(arbi, conversationId)
6970
+ ]);
6971
+ const plan = conversation.plan?.steps ?? [];
6972
+ if (!plan.length)
6973
+ throw new Error(`${conversationId} is not a stepped workflow run (its plan has no steps)`);
6974
+ const steps = runSteps(plan, activity);
6975
+ return { conversation, steps, waitingOn: runWaitingOn(steps) };
6976
+ }
6977
+ async function getStepAnswers(arbi, conversationId, responseIds) {
6978
+ if (!responseIds.length) return {};
6979
+ const data = requireData(
6980
+ await arbi.fetch.GET("/v1/conversation/{conversation_ext_id}/threads", byId(conversationId)),
6981
+ "Failed to fetch the thread"
6982
+ );
6983
+ const wanted = new Set(responseIds);
6984
+ const out = {};
6985
+ for (const thread of data.threads ?? [])
6986
+ for (const m2 of thread.history ?? [])
6987
+ if (wanted.has(m2.external_id) && typeof m2.content === "string") out[m2.external_id] = m2.content;
6988
+ return out;
6989
+ }
6702
6990
 
6703
6991
  // src/operations/workflows.ts
6704
6992
  var workflows_exports = {};
@@ -8909,6 +9197,7 @@ exports.ClaudeOrchestrator = ClaudeOrchestrator;
8909
9197
  exports.DOC_TERMINAL_STATUSES = DOC_TERMINAL_STATUSES;
8910
9198
  exports.FileConfigStore = FileConfigStore;
8911
9199
  exports.OpenClawOrchestrator = OpenClawOrchestrator;
9200
+ exports.PlanStepsError = PlanStepsError;
8912
9201
  exports.WebSocketAuthError = WebSocketAuthError;
8913
9202
  exports.account = account_exports;
8914
9203
  exports.agentconfig = agentconfig_exports;
@@ -8978,6 +9267,7 @@ exports.stripCitationMarkdown = stripCitationMarkdown;
8978
9267
  exports.summarizeCitations = summarizeCitations;
8979
9268
  exports.tags = tags_exports;
8980
9269
  exports.work = work_exports;
9270
+ exports.workflowSteps = workflow_steps_exports;
8981
9271
  exports.workflows = workflows_exports;
8982
9272
  exports.workspaces = workspaces_exports;
8983
9273
  //# sourceMappingURL=index.cjs.map