@deliverd/sdk 0.11.0 → 0.12.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 +39 -0
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +398 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +13 -0
- package/dist/released.d.ts +1 -1
- package/dist/released.d.ts.map +1 -1
- package/dist/released.js +1262 -0
- package/dist/tasks.d.ts +95 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +254 -0
- package/dist/types.d.ts +341 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/dev.ts +407 -1
- package/src/index.ts +4 -0
- package/src/messages.ts +13 -0
- package/src/released.ts +1262 -0
- package/src/tasks.ts +312 -0
- package/src/types.ts +398 -1
package/src/dev.ts
CHANGED
|
@@ -5,6 +5,11 @@ import type {
|
|
|
5
5
|
FlowData,
|
|
6
6
|
GateData,
|
|
7
7
|
ReviewData,
|
|
8
|
+
TaskData,
|
|
9
|
+
TaskFinding,
|
|
10
|
+
TaskLink,
|
|
11
|
+
TaskNode,
|
|
12
|
+
TaskState,
|
|
8
13
|
} from "./types.js";
|
|
9
14
|
|
|
10
15
|
/**
|
|
@@ -88,6 +93,7 @@ export function developmentFetch(options: DevelopmentOptions = {}): typeof globa
|
|
|
88
93
|
const gateStore = new Map<string, StoredGate>();
|
|
89
94
|
const collectionStore = new Map<string, StoredCollection>();
|
|
90
95
|
const flowStore = new Map<string, FlowData>();
|
|
96
|
+
const tasks = developmentTasks(options);
|
|
91
97
|
|
|
92
98
|
return (async (input: string | URL | Request, init?: RequestInit) => {
|
|
93
99
|
const url = new URL(String(input));
|
|
@@ -632,11 +638,35 @@ export function developmentFetch(options: DevelopmentOptions = {}): typeof globa
|
|
|
632
638
|
return json({ flows: externalId ? all.filter((f) => f.externalId === externalId) : all });
|
|
633
639
|
}
|
|
634
640
|
|
|
641
|
+
/*
|
|
642
|
+
* Task Graph in development: real objects with no simulation attached,
|
|
643
|
+
* the same reasoning as flows. Nothing here is decided by anybody, so
|
|
644
|
+
* there is nobody to imagine — what is worth running offline is the loop
|
|
645
|
+
* itself: start an objective, branch a side task, complete it, apply it,
|
|
646
|
+
* and read the context the parent comes back with. Putting a decision to
|
|
647
|
+
* an approval is the one call left to the real API, because the approval
|
|
648
|
+
* it raises lives in a store this simulation does not join to.
|
|
649
|
+
*/
|
|
650
|
+
if (method === "POST" && path === "/tasks") {
|
|
651
|
+
const { task, resumed } = tasks.create(body);
|
|
652
|
+
return json({ ...task, resumed }, resumed ? 200 : 201);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (method === "GET" && path === "/tasks") {
|
|
656
|
+
return json({ tasks: tasks.list(url.searchParams), nextCursor: null });
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
const taskMatch = /^\/tasks\/([^/]+)(?:\/(context|graph|side-tasks|findings|complete|apply|reopen|cancel|links))?$/.exec(path);
|
|
660
|
+
if (taskMatch) {
|
|
661
|
+
const [status, payload] = tasks.act(method, taskMatch[1]!, taskMatch[2] ?? null, body);
|
|
662
|
+
return json(payload, status);
|
|
663
|
+
}
|
|
664
|
+
|
|
635
665
|
return json(
|
|
636
666
|
{
|
|
637
667
|
error: {
|
|
638
668
|
code: "not_in_development_mode",
|
|
639
|
-
message: `Development mode simulates approvals, reviews and
|
|
669
|
+
message: `Development mode simulates approvals, reviews, collections, flows and tasks only, and something called ${method} ${path}. Use a real API key for the rest of the API.`,
|
|
640
670
|
},
|
|
641
671
|
},
|
|
642
672
|
501
|
|
@@ -866,3 +896,379 @@ function announceCollection(data: CollectionData, outcome: string): void {
|
|
|
866
896
|
];
|
|
867
897
|
console.log(lines.join("\n"));
|
|
868
898
|
}
|
|
899
|
+
|
|
900
|
+
/* ── Tasks ────────────────────────────────────────────────────────────────── */
|
|
901
|
+
|
|
902
|
+
type TaskReply = [status: number, payload: unknown];
|
|
903
|
+
|
|
904
|
+
const notFound = (what: string): TaskReply => [
|
|
905
|
+
404,
|
|
906
|
+
{ error: { code: "not_found", message: `No such ${what} in this development session.` } },
|
|
907
|
+
];
|
|
908
|
+
const refused = (code: string, message: string, status = 409): TaskReply => [
|
|
909
|
+
status,
|
|
910
|
+
{ error: { code, message } },
|
|
911
|
+
];
|
|
912
|
+
|
|
913
|
+
function strings(value: unknown): string[] {
|
|
914
|
+
return Array.isArray(value) ? value.filter((v): v is string => typeof v === "string" && v.trim() !== "") : [];
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
function mergeState(current: TaskState, patch: Record<string, unknown> | undefined): TaskState {
|
|
918
|
+
if (!patch) return current;
|
|
919
|
+
const next: TaskState = { ...current };
|
|
920
|
+
if ("currentState" in patch) next.currentState = (patch.currentState as string | null) ?? null;
|
|
921
|
+
if ("nextAction" in patch) next.nextAction = (patch.nextAction as string | null) ?? null;
|
|
922
|
+
if (patch.openQuestions) next.openQuestions = strings(patch.openQuestions);
|
|
923
|
+
if (patch.constraints) next.constraints = strings(patch.constraints);
|
|
924
|
+
if (patch.blockedBy) next.blockedBy = strings(patch.blockedBy);
|
|
925
|
+
for (const q of strings(patch.addOpenQuestions)) {
|
|
926
|
+
if (!next.openQuestions.includes(q)) next.openQuestions = [...next.openQuestions, q];
|
|
927
|
+
}
|
|
928
|
+
const gone = new Set(strings(patch.resolveOpenQuestions).map((q) => q.toLowerCase()));
|
|
929
|
+
if (gone.size) next.openQuestions = next.openQuestions.filter((q) => !gone.has(q.toLowerCase()));
|
|
930
|
+
return next;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* The Task Graph, in memory: enough of the server's rules that the loop an
|
|
935
|
+
* agent actually runs — objective, side task, result, apply, context — does
|
|
936
|
+
* offline what it does live. Not the whole service: no workspaces, no
|
|
937
|
+
* permissions, no audit, and a context assembled from what this store holds.
|
|
938
|
+
*/
|
|
939
|
+
function developmentTasks(options: DevelopmentOptions) {
|
|
940
|
+
const store = new Map<string, TaskData>();
|
|
941
|
+
const findings = new Map<string, TaskFinding[]>();
|
|
942
|
+
const links: TaskLink[] = [];
|
|
943
|
+
|
|
944
|
+
const active = (t: TaskData) => t.status !== "completed" && t.status !== "cancelled";
|
|
945
|
+
const childrenOf = (id: string) => [...store.values()].filter((t) => t.parentTaskId === id);
|
|
946
|
+
const node = (t: TaskData): TaskNode => ({
|
|
947
|
+
id: t.id,
|
|
948
|
+
parentTaskId: t.parentTaskId,
|
|
949
|
+
kind: t.kind,
|
|
950
|
+
depth: t.depth,
|
|
951
|
+
status: t.status,
|
|
952
|
+
title: t.title,
|
|
953
|
+
outcome: t.outcome,
|
|
954
|
+
resultSummary: t.result?.summary ?? null,
|
|
955
|
+
url: t.url,
|
|
956
|
+
});
|
|
957
|
+
const pathTo = (t: TaskData) => {
|
|
958
|
+
const path = [];
|
|
959
|
+
for (let at: TaskData | undefined = t; at; at = at.parentTaskId ? store.get(at.parentTaskId) : undefined) {
|
|
960
|
+
path.unshift({ id: at.id, title: at.title, kind: at.kind });
|
|
961
|
+
}
|
|
962
|
+
return path;
|
|
963
|
+
};
|
|
964
|
+
const bump = (t: TaskData) => {
|
|
965
|
+
t.version += 1;
|
|
966
|
+
t.updatedAt = new Date().toISOString();
|
|
967
|
+
};
|
|
968
|
+
const listed = (t: TaskData): TaskData => {
|
|
969
|
+
const { sideTasks: _counts, path: _path, children: _children, findings: _findings, links: _links, resumed: _resumed, ...rest } = t;
|
|
970
|
+
return rest;
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
function make(body: Record<string, unknown>, parent: TaskData | null): TaskData {
|
|
974
|
+
const id = `tsk_dev_${++counter}`;
|
|
975
|
+
const now = new Date().toISOString();
|
|
976
|
+
const parentDecisions = parent
|
|
977
|
+
? (findings.get(parent.id) ?? []).filter((f) => f.kind === "decision" && f.decisionStatus !== "rejected")
|
|
978
|
+
: [];
|
|
979
|
+
return {
|
|
980
|
+
id,
|
|
981
|
+
kind: parent ? ((body.kind as TaskData["kind"] | undefined) ?? "side_task") : "objective",
|
|
982
|
+
status: "open",
|
|
983
|
+
url: `https://deliverd.dev/tasks/${id}`,
|
|
984
|
+
title: String(body.title ?? "Untitled"),
|
|
985
|
+
objective: String((parent ? body.goal : body.objective) ?? ""),
|
|
986
|
+
rootTaskId: parent ? parent.rootTaskId : id,
|
|
987
|
+
parentTaskId: parent ? parent.id : null,
|
|
988
|
+
depth: parent ? parent.depth + 1 : 0,
|
|
989
|
+
workspaceId: parent ? parent.workspaceId : ((body.workspaceId as string | undefined) ?? null),
|
|
990
|
+
flowId: parent ? parent.flowId : ((body.flowId as string | undefined) ?? null),
|
|
991
|
+
state: mergeState(
|
|
992
|
+
{ currentState: null, openQuestions: [], constraints: [], blockedBy: [], nextAction: null },
|
|
993
|
+
body.state as Record<string, unknown> | undefined
|
|
994
|
+
),
|
|
995
|
+
snapshot: parent
|
|
996
|
+
? {
|
|
997
|
+
rootObjective: store.get(parent.rootTaskId)?.objective ?? parent.objective,
|
|
998
|
+
parentObjective: parent.objective,
|
|
999
|
+
sideTaskGoal: String(body.goal ?? ""),
|
|
1000
|
+
relevantContext: body.relevantContext
|
|
1001
|
+
? strings(body.relevantContext)
|
|
1002
|
+
: parent.state.currentState
|
|
1003
|
+
? [parent.state.currentState]
|
|
1004
|
+
: [],
|
|
1005
|
+
knownConstraints: body.knownConstraints ? strings(body.knownConstraints) : parent.state.constraints,
|
|
1006
|
+
relatedDecisions: parentDecisions.map((d) =>
|
|
1007
|
+
d.decisionStatus === "confirmed" ? d.title : `${d.title} (suggested)`
|
|
1008
|
+
),
|
|
1009
|
+
resolvesQuestion: (body.resolvesQuestion as string | undefined) ?? null,
|
|
1010
|
+
}
|
|
1011
|
+
: null,
|
|
1012
|
+
result: null,
|
|
1013
|
+
previousResults: [],
|
|
1014
|
+
outcome: null,
|
|
1015
|
+
origin: (body.origin as TaskData["origin"] | undefined) ?? "manual",
|
|
1016
|
+
externalId: parent ? null : ((body.externalId as string | undefined) ?? null),
|
|
1017
|
+
sessionUrl: (body.sessionUrl as string | undefined) ?? null,
|
|
1018
|
+
version: 1,
|
|
1019
|
+
startedBy: "You (development mode)",
|
|
1020
|
+
createdAt: now,
|
|
1021
|
+
updatedAt: now,
|
|
1022
|
+
completedAt: null,
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function context(t: TaskData) {
|
|
1027
|
+
const root = store.get(t.rootTaskId) ?? t;
|
|
1028
|
+
const isSideTask = t.kind !== "objective";
|
|
1029
|
+
const own = findings.get(t.id) ?? [];
|
|
1030
|
+
const decisions = [...(t.id === root.id ? [] : (findings.get(root.id) ?? [])), ...own].filter(
|
|
1031
|
+
(f) => f.kind === "decision"
|
|
1032
|
+
);
|
|
1033
|
+
const children = childrenOf(t.id);
|
|
1034
|
+
const finished = children.filter((c) => c.status === "completed");
|
|
1035
|
+
const awaiting = finished.filter((c) => c.outcome === null);
|
|
1036
|
+
const next = t.state.nextAction ?? (isSideTask ? null : root.state.nextAction);
|
|
1037
|
+
return {
|
|
1038
|
+
rootObjective: root.objective,
|
|
1039
|
+
task: { id: t.id, kind: t.kind, title: t.title, objective: t.objective, status: t.status, isSideTask },
|
|
1040
|
+
path: pathTo(t),
|
|
1041
|
+
currentState: t.state.currentState,
|
|
1042
|
+
nextAction: next,
|
|
1043
|
+
openQuestions: t.state.openQuestions,
|
|
1044
|
+
constraints: t.state.constraints.length ? t.state.constraints : (t.snapshot?.knownConstraints ?? []),
|
|
1045
|
+
blockedBy: t.state.blockedBy,
|
|
1046
|
+
decisions: {
|
|
1047
|
+
confirmed: decisions.filter((d) => d.decisionStatus === "confirmed").map((d) => d.title),
|
|
1048
|
+
suggested: decisions.filter((d) => d.decisionStatus === "suggested").map((d) => d.title),
|
|
1049
|
+
},
|
|
1050
|
+
snapshot: t.snapshot,
|
|
1051
|
+
relevantFindings: own
|
|
1052
|
+
.filter((f) => f.kind !== "decision")
|
|
1053
|
+
.slice(0, 12)
|
|
1054
|
+
.map((f) => ({ kind: f.kind, title: f.title, body: f.body })),
|
|
1055
|
+
sideTasks: {
|
|
1056
|
+
active: children.filter(active).map((c) => ({ id: c.id, title: c.title, kind: c.kind, status: c.status })),
|
|
1057
|
+
awaitingOutcome: awaiting.map((c) => ({ id: c.id, title: c.title, summary: c.result?.summary ?? null })),
|
|
1058
|
+
completed: finished
|
|
1059
|
+
.filter((c) => c.outcome !== null)
|
|
1060
|
+
.map((c) => ({ id: c.id, title: c.title, summary: c.result?.summary ?? null, outcome: c.outcome })),
|
|
1061
|
+
},
|
|
1062
|
+
reanchor: [
|
|
1063
|
+
`Objective: ${root.objective}`,
|
|
1064
|
+
isSideTask ? `You are in a side task: ${t.title}. Its goal: ${t.objective}` : null,
|
|
1065
|
+
t.state.currentState ? `Where it stands: ${t.state.currentState}` : null,
|
|
1066
|
+
next ? `Next: ${next}` : null,
|
|
1067
|
+
]
|
|
1068
|
+
.filter(Boolean)
|
|
1069
|
+
.join("\n"),
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
function addFinding(t: TaskData, body: Record<string, unknown>, sourceTaskId: string | null = null): TaskFinding {
|
|
1074
|
+
const kind = (body.kind as TaskFinding["kind"] | undefined) ?? "finding";
|
|
1075
|
+
const finding: TaskFinding = {
|
|
1076
|
+
id: `fnd_dev_${++counter}`,
|
|
1077
|
+
taskId: t.id,
|
|
1078
|
+
kind,
|
|
1079
|
+
title: String(body.title ?? "Untitled"),
|
|
1080
|
+
body: String(body.body ?? ""),
|
|
1081
|
+
evidence: strings(body.evidence),
|
|
1082
|
+
decisionStatus: kind === "decision" ? "suggested" : null,
|
|
1083
|
+
decidedBy: null,
|
|
1084
|
+
decidedAt: null,
|
|
1085
|
+
approvalId: null,
|
|
1086
|
+
approvalStatus: null,
|
|
1087
|
+
sourceTaskId,
|
|
1088
|
+
createdBy: "You (development mode)",
|
|
1089
|
+
createdAt: new Date().toISOString(),
|
|
1090
|
+
};
|
|
1091
|
+
findings.set(t.id, [finding, ...(findings.get(t.id) ?? [])]);
|
|
1092
|
+
return finding;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
return {
|
|
1096
|
+
create(body: Record<string, unknown>): { task: TaskData; resumed: boolean } {
|
|
1097
|
+
const externalId = body.externalId as string | undefined;
|
|
1098
|
+
if (externalId) {
|
|
1099
|
+
const existing = [...store.values()].find(
|
|
1100
|
+
(t) => t.kind === "objective" && t.externalId === externalId && active(t)
|
|
1101
|
+
);
|
|
1102
|
+
if (existing) return { task: listed(existing), resumed: true };
|
|
1103
|
+
}
|
|
1104
|
+
const task = make(body, null);
|
|
1105
|
+
store.set(task.id, task);
|
|
1106
|
+
if (options.log !== false) console.log(`\n ▸ Objective: ${task.title}\n ${task.url}\n`);
|
|
1107
|
+
return { task: listed(task), resumed: false };
|
|
1108
|
+
},
|
|
1109
|
+
|
|
1110
|
+
list(query: URLSearchParams): TaskData[] {
|
|
1111
|
+
const status = query.get("status");
|
|
1112
|
+
const externalId = query.get("externalId");
|
|
1113
|
+
const q = query.get("q")?.toLowerCase();
|
|
1114
|
+
return [...store.values()]
|
|
1115
|
+
.filter((t) => t.kind === "objective")
|
|
1116
|
+
.filter((t) => !status || (status === "active" ? active(t) : t.status === status))
|
|
1117
|
+
.filter((t) => !externalId || t.externalId === externalId)
|
|
1118
|
+
.filter((t) => !q || `${t.title} ${t.objective}`.toLowerCase().includes(q))
|
|
1119
|
+
.map((t) => {
|
|
1120
|
+
const below = [...store.values()].filter((s) => s.rootTaskId === t.id && s.id !== t.id);
|
|
1121
|
+
return {
|
|
1122
|
+
...listed(t),
|
|
1123
|
+
sideTasks: {
|
|
1124
|
+
open: below.filter(active).length,
|
|
1125
|
+
done: below.filter((s) => s.status === "completed" && s.outcome !== null).length,
|
|
1126
|
+
awaiting: below.filter((s) => s.status === "completed" && s.outcome === null).length,
|
|
1127
|
+
},
|
|
1128
|
+
};
|
|
1129
|
+
});
|
|
1130
|
+
},
|
|
1131
|
+
|
|
1132
|
+
act(method: string, id: string, action: string | null, body: Record<string, unknown>): TaskReply {
|
|
1133
|
+
const t = store.get(id);
|
|
1134
|
+
if (!t) return notFound("task");
|
|
1135
|
+
|
|
1136
|
+
if (method === "GET" && action === null) {
|
|
1137
|
+
return [
|
|
1138
|
+
200,
|
|
1139
|
+
{
|
|
1140
|
+
...listed(t),
|
|
1141
|
+
path: pathTo(t),
|
|
1142
|
+
children: childrenOf(t.id).map(node),
|
|
1143
|
+
findings: findings.get(t.id) ?? [],
|
|
1144
|
+
links: links.filter((l) => l.sourceTaskId === t.id || l.targetTaskId === t.id),
|
|
1145
|
+
},
|
|
1146
|
+
];
|
|
1147
|
+
}
|
|
1148
|
+
if (method === "GET" && action === "context") return [200, context(t)];
|
|
1149
|
+
if (method === "GET" && action === "graph") {
|
|
1150
|
+
const root = store.get(t.rootTaskId) ?? t;
|
|
1151
|
+
const tree = [...store.values()].filter((n) => n.rootTaskId === root.id);
|
|
1152
|
+
const ids = new Set(tree.map((n) => n.id));
|
|
1153
|
+
return [
|
|
1154
|
+
200,
|
|
1155
|
+
{
|
|
1156
|
+
root: listed(root),
|
|
1157
|
+
nodes: tree.map(node),
|
|
1158
|
+
links: links.filter((l) => ids.has(l.sourceTaskId) || ids.has(l.targetTaskId)),
|
|
1159
|
+
},
|
|
1160
|
+
];
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
if (method === "PATCH" && action === null) {
|
|
1164
|
+
if (typeof body.expectedVersion === "number" && body.expectedVersion !== t.version) {
|
|
1165
|
+
return refused("version_conflict", "The task changed since you read it. Read it again and retry.");
|
|
1166
|
+
}
|
|
1167
|
+
if (typeof body.title === "string") t.title = body.title;
|
|
1168
|
+
if (typeof body.objective === "string") t.objective = body.objective;
|
|
1169
|
+
if (typeof body.status === "string") t.status = body.status as TaskData["status"];
|
|
1170
|
+
if ("sessionUrl" in body) t.sessionUrl = (body.sessionUrl as string | null) ?? null;
|
|
1171
|
+
t.state = mergeState(t.state, body.state as Record<string, unknown> | undefined);
|
|
1172
|
+
bump(t);
|
|
1173
|
+
return [200, listed(t)];
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
if (method !== "POST") return refused("not_in_development_mode", `${method} ${action ?? ""} is not simulated.`, 501);
|
|
1177
|
+
|
|
1178
|
+
switch (action) {
|
|
1179
|
+
case "side-tasks": {
|
|
1180
|
+
if (!active(t)) return refused("not_open", "The task this belongs to is finished. Reopen it first.");
|
|
1181
|
+
if (t.depth >= 3) {
|
|
1182
|
+
return refused("invalid", "Side tasks nest at most three deep. Branch from a task higher up instead.", 400);
|
|
1183
|
+
}
|
|
1184
|
+
const side = make(body, t);
|
|
1185
|
+
store.set(side.id, side);
|
|
1186
|
+
if (options.log !== false) console.log(`\n ↳ Side task: ${side.title}\n ${side.url}\n`);
|
|
1187
|
+
return [201, listed(side)];
|
|
1188
|
+
}
|
|
1189
|
+
case "findings":
|
|
1190
|
+
return [201, addFinding(t, body)];
|
|
1191
|
+
case "complete": {
|
|
1192
|
+
if (!active(t)) return refused("not_open", "This task is already complete or cancelled.");
|
|
1193
|
+
t.result = {
|
|
1194
|
+
summary: String(body.summary ?? ""),
|
|
1195
|
+
findings: (body.findings as NonNullable<TaskData["result"]>["findings"] | undefined) ?? [],
|
|
1196
|
+
decisions: (body.decisions as NonNullable<TaskData["result"]>["decisions"] | undefined) ?? [],
|
|
1197
|
+
recommendations: strings(body.recommendations),
|
|
1198
|
+
unresolvedQuestions: strings(body.unresolvedQuestions),
|
|
1199
|
+
parentImpact: (body.parentImpact as string | undefined) ?? null,
|
|
1200
|
+
suggestedNextAction: (body.suggestedNextAction as string | undefined) ?? null,
|
|
1201
|
+
};
|
|
1202
|
+
for (const f of t.result.findings) addFinding(t, { kind: "finding", title: f.title, body: f.description, evidence: f.evidence });
|
|
1203
|
+
for (const d of t.result.decisions) addFinding(t, { kind: "decision", title: d.title, body: d.rationale });
|
|
1204
|
+
t.status = "completed";
|
|
1205
|
+
t.completedAt = new Date().toISOString();
|
|
1206
|
+
bump(t);
|
|
1207
|
+
return [200, listed(t)];
|
|
1208
|
+
}
|
|
1209
|
+
case "apply": {
|
|
1210
|
+
const parent = t.parentTaskId ? store.get(t.parentTaskId) : undefined;
|
|
1211
|
+
if (!parent) return refused("invalid", "An objective has no parent to apply to.", 400);
|
|
1212
|
+
if (t.status !== "completed" || !t.result) {
|
|
1213
|
+
return refused("not_completed", "This side task has not been completed yet, so there is no result to use.");
|
|
1214
|
+
}
|
|
1215
|
+
if (t.outcome !== null) {
|
|
1216
|
+
return refused("already_applied", "This result has already been applied to the main task. Reopen the side task to change it.");
|
|
1217
|
+
}
|
|
1218
|
+
const outcome = (body.outcome as TaskData["outcome"] | undefined) ?? "applied";
|
|
1219
|
+
t.outcome = outcome;
|
|
1220
|
+
bump(t);
|
|
1221
|
+
if (outcome === "applied") {
|
|
1222
|
+
for (const f of findings.get(t.id) ?? []) {
|
|
1223
|
+
addFinding(parent, { kind: f.kind, title: f.title, body: f.body, evidence: f.evidence }, t.id);
|
|
1224
|
+
}
|
|
1225
|
+
const resolves = t.snapshot?.resolvesQuestion;
|
|
1226
|
+
parent.state = mergeState(parent.state, {
|
|
1227
|
+
addOpenQuestions: t.result.unresolvedQuestions,
|
|
1228
|
+
...(resolves ? { resolveOpenQuestions: [resolves] } : {}),
|
|
1229
|
+
...(t.result.suggestedNextAction ? { nextAction: t.result.suggestedNextAction } : {}),
|
|
1230
|
+
});
|
|
1231
|
+
bump(parent);
|
|
1232
|
+
}
|
|
1233
|
+
const message =
|
|
1234
|
+
outcome === "applied"
|
|
1235
|
+
? `The result of "${t.title}" is now part of "${parent.title}".`
|
|
1236
|
+
: outcome === "kept"
|
|
1237
|
+
? `"${t.title}" is kept for reference; nothing in "${parent.title}" changed.`
|
|
1238
|
+
: `"${t.title}" was discarded; nothing in "${parent.title}" changed.`;
|
|
1239
|
+
return [200, { sideTask: listed(t), parent: listed(parent), message, context: context(parent) }];
|
|
1240
|
+
}
|
|
1241
|
+
case "reopen": {
|
|
1242
|
+
if (t.status !== "completed" && t.status !== "cancelled") {
|
|
1243
|
+
return refused("not_open", "Only a completed or cancelled task can be reopened.");
|
|
1244
|
+
}
|
|
1245
|
+
if (t.result) t.previousResults = [t.result, ...t.previousResults];
|
|
1246
|
+
t.result = null;
|
|
1247
|
+
t.outcome = null;
|
|
1248
|
+
t.status = "open";
|
|
1249
|
+
t.completedAt = null;
|
|
1250
|
+
bump(t);
|
|
1251
|
+
return [200, listed(t)];
|
|
1252
|
+
}
|
|
1253
|
+
case "cancel": {
|
|
1254
|
+
if (!active(t)) return refused("not_open", "This task is already complete or cancelled.");
|
|
1255
|
+
t.status = "cancelled";
|
|
1256
|
+
t.completedAt = new Date().toISOString();
|
|
1257
|
+
bump(t);
|
|
1258
|
+
return [200, listed(t)];
|
|
1259
|
+
}
|
|
1260
|
+
case "links": {
|
|
1261
|
+
const targetTaskId = String(body.targetTaskId ?? "");
|
|
1262
|
+
if (targetTaskId === t.id) return refused("invalid", "A task cannot be linked to itself.", 400);
|
|
1263
|
+
if (!store.has(targetTaskId)) return notFound("task");
|
|
1264
|
+
const kind = body.kind as TaskLink["kind"];
|
|
1265
|
+
const exists = links.some((l) => l.sourceTaskId === t.id && l.targetTaskId === targetTaskId && l.kind === kind);
|
|
1266
|
+
if (!exists) links.push({ sourceTaskId: t.id, targetTaskId, kind });
|
|
1267
|
+
return [201, { linked: true, sourceTaskId: t.id, targetTaskId, kind, created: !exists }];
|
|
1268
|
+
}
|
|
1269
|
+
default:
|
|
1270
|
+
return refused("not_in_development_mode", `POST /tasks/${id}/${action ?? ""} is not simulated.`, 501);
|
|
1271
|
+
}
|
|
1272
|
+
},
|
|
1273
|
+
};
|
|
1274
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { Comments, Reports, Schedules } from "./reports.js";
|
|
|
5
5
|
import { Reviews, type ReviewWaitOptions } from "./reviews.js";
|
|
6
6
|
import { Collections, type CollectWaitOptions } from "./collections.js";
|
|
7
7
|
import { Flows } from "./flows.js";
|
|
8
|
+
import { Tasks } from "./tasks.js";
|
|
8
9
|
import { Webhooks } from "./webhooks.js";
|
|
9
10
|
import { developmentFetch, type DevelopmentOptions } from "./dev.js";
|
|
10
11
|
import { durationMs, type Duration } from "./duration.js";
|
|
@@ -27,6 +28,7 @@ export { Comments, Reports, Schedules } from "./reports.js";
|
|
|
27
28
|
export { Review, Reviews, type ReviewWaitOptions } from "./reviews.js";
|
|
28
29
|
export { Collection, Collections, type CollectWaitOptions } from "./collections.js";
|
|
29
30
|
export { Flow, Flows, type FlowPrimitives } from "./flows.js";
|
|
31
|
+
export { Task, Tasks } from "./tasks.js";
|
|
30
32
|
export {
|
|
31
33
|
constructCallback,
|
|
32
34
|
constructEvent,
|
|
@@ -141,6 +143,7 @@ export class Deliverd {
|
|
|
141
143
|
readonly reviews: Reviews;
|
|
142
144
|
readonly collections: Collections;
|
|
143
145
|
readonly flows: Flows;
|
|
146
|
+
readonly tasks: Tasks;
|
|
144
147
|
readonly reports: Reports;
|
|
145
148
|
readonly comments: Comments;
|
|
146
149
|
readonly schedules: Schedules;
|
|
@@ -171,6 +174,7 @@ export class Deliverd {
|
|
|
171
174
|
* implementation that can drift from it.
|
|
172
175
|
*/
|
|
173
176
|
this.flows = new Flows(this.http, this);
|
|
177
|
+
this.tasks = new Tasks(this.http);
|
|
174
178
|
this.reports = new Reports(this.http);
|
|
175
179
|
this.comments = new Comments(this.http);
|
|
176
180
|
this.schedules = new Schedules(this.http);
|
package/src/messages.ts
CHANGED
|
@@ -199,6 +199,19 @@ export const EXPLANATIONS: Record<string, Explanation> = {
|
|
|
199
199
|
invalid_signature: {
|
|
200
200
|
hint: "Check the endpoint's signing secret, and that you passed the raw request body rather than a re-encoded object.",
|
|
201
201
|
},
|
|
202
|
+
/*
|
|
203
|
+
* The Task Graph's refusals. The server's sentences already say what
|
|
204
|
+
* happened; these say what to do, which for each is one specific call.
|
|
205
|
+
*/
|
|
206
|
+
version_conflict: {
|
|
207
|
+
hint: "Somebody wrote to the task between your read and your write. Read it again (`task.refresh()`), reapply your change to what is there now, and pass the new `expectedVersion`.",
|
|
208
|
+
},
|
|
209
|
+
not_completed: {
|
|
210
|
+
hint: "Only a completed side task has a result to apply. Finish it with `complete()` first; a cancelled one has to be reopened and completed.",
|
|
211
|
+
},
|
|
212
|
+
already_applied: {
|
|
213
|
+
hint: "Applying happens once, so the parent is never given the same decisions twice. To change what was carried up, `reopen()` the side task, complete it again and apply the new result.",
|
|
214
|
+
},
|
|
202
215
|
};
|
|
203
216
|
|
|
204
217
|
/** The message as a developer should read it: what happened, then what to do. */
|