@mitralab.io/sdk-core 0.3.10 → 0.3.11
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/CHANGELOG.md +58 -0
- package/README.md +5 -0
- package/contracts/README.md +6 -0
- package/contracts/manifest.json +6 -1
- package/contracts/v0.3.11/sdk-parity.json +2902 -0
- package/dist/index.cjs +167 -28
- package/dist/index.d.cts +235 -2
- package/dist/index.d.ts +235 -2
- package/dist/index.js +166 -28
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -980,7 +980,40 @@ function expectAgentTask(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
|
980
980
|
if (typeof task.archived !== "boolean") invalidField(context, "archived", errors);
|
|
981
981
|
if (!isNullableString(task.createdAt)) invalidField(context, "createdAt", errors);
|
|
982
982
|
if (typeof task.updatedAt !== "string") invalidField(context, "updatedAt", errors);
|
|
983
|
-
|
|
983
|
+
const { turn: rawTurn, instructions: rawInstructions, folderKey: rawFolderKey, ...rest } = task;
|
|
984
|
+
const turn = rawTurn === null ? null : agentTaskTurnOf(rawTurn);
|
|
985
|
+
const instructions = typeof rawInstructions === "string" ? rawInstructions : void 0;
|
|
986
|
+
const folderKey = typeof rawFolderKey === "string" ? rawFolderKey : void 0;
|
|
987
|
+
return {
|
|
988
|
+
...rest,
|
|
989
|
+
...turn !== void 0 ? { turn } : {},
|
|
990
|
+
...instructions !== void 0 ? { instructions } : {},
|
|
991
|
+
...folderKey !== void 0 ? { folderKey } : {}
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
function agentTaskTurnOf(value) {
|
|
995
|
+
if (!isObject(value)) return void 0;
|
|
996
|
+
if (typeof value.state !== "string") return void 0;
|
|
997
|
+
if (!isNullableString(value.currentTurnId) || !isCount(value.queuedCount)) return void 0;
|
|
998
|
+
if (value.lastTurn === null) return value;
|
|
999
|
+
const lastTurn = value.lastTurn;
|
|
1000
|
+
if (!isObject(lastTurn)) return void 0;
|
|
1001
|
+
if (typeof lastTurn.turnId !== "string" || typeof lastTurn.status !== "string") return void 0;
|
|
1002
|
+
if (!isStringArray(lastTurn.messageIds)) return void 0;
|
|
1003
|
+
const nullableTexts = ["finishReason", "errorCode", "startedAt", "endedAt"];
|
|
1004
|
+
if (!nullableTexts.every((field) => isNullableString(lastTurn[field]))) return void 0;
|
|
1005
|
+
return value;
|
|
1006
|
+
}
|
|
1007
|
+
function expectAgentTaskTurnStates(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
1008
|
+
return expectObjectArray(value, context, errors).map((state, position) => {
|
|
1009
|
+
if (typeof state.taskId !== "string") {
|
|
1010
|
+
invalidField(`${context} item ${position}`, "taskId", errors);
|
|
1011
|
+
}
|
|
1012
|
+
return {
|
|
1013
|
+
...state,
|
|
1014
|
+
turn: agentTaskTurnOf(state.turn) ?? null
|
|
1015
|
+
};
|
|
1016
|
+
});
|
|
984
1017
|
}
|
|
985
1018
|
function expectAgentTaskChannel(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
986
1019
|
if (value === void 0 || value === null || value === "") return null;
|
|
@@ -1054,6 +1087,22 @@ function agentTurnUsageOf(value) {
|
|
|
1054
1087
|
}
|
|
1055
1088
|
return usage;
|
|
1056
1089
|
}
|
|
1090
|
+
function agentTurnCompactionsOf(value) {
|
|
1091
|
+
if (!Array.isArray(value)) return [];
|
|
1092
|
+
return value.flatMap((item) => {
|
|
1093
|
+
const compaction = agentTurnCompactionOf(item);
|
|
1094
|
+
return compaction ? [compaction] : [];
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
function agentTurnCompactionOf(value) {
|
|
1098
|
+
if (!isObject(value) || typeof value.trigger !== "string") return void 0;
|
|
1099
|
+
return {
|
|
1100
|
+
trigger: value.trigger,
|
|
1101
|
+
tokensBefore: isCount(value.tokensBefore) ? value.tokensBefore : null,
|
|
1102
|
+
tokensAfter: isCount(value.tokensAfter) ? value.tokensAfter : null,
|
|
1103
|
+
at: typeof value.at === "string" ? value.at : null
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1057
1106
|
function agentTurnUsageRequestOf(value) {
|
|
1058
1107
|
if (!isObject(value)) return void 0;
|
|
1059
1108
|
const request = {};
|
|
@@ -1692,6 +1741,8 @@ function createAgentsModule(functionsTransport, copilotTransport, errors = defau
|
|
|
1692
1741
|
}
|
|
1693
1742
|
|
|
1694
1743
|
// src/modules/agentTasks.ts
|
|
1744
|
+
var TURN_STATES_BATCH_SIZE = 100;
|
|
1745
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
1695
1746
|
function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory) {
|
|
1696
1747
|
const path = (id) => `/api/v1/tasks/${encodePathSegment(id, "task id", errors)}`;
|
|
1697
1748
|
return {
|
|
@@ -1705,12 +1756,18 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1705
1756
|
search: options.search,
|
|
1706
1757
|
userId: options.userId
|
|
1707
1758
|
};
|
|
1708
|
-
|
|
1759
|
+
const context = "Agent task page response";
|
|
1760
|
+
const page = expectPage(
|
|
1709
1761
|
await transport.request("/api/v1/tasks", { method: "GET", params }),
|
|
1710
|
-
|
|
1711
|
-
errors
|
|
1712
|
-
expectAgentTask
|
|
1762
|
+
context,
|
|
1763
|
+
errors
|
|
1713
1764
|
);
|
|
1765
|
+
return {
|
|
1766
|
+
...page,
|
|
1767
|
+
content: page.content.map(
|
|
1768
|
+
(task, position) => expectAgentTask(task, `${context} item ${position}`, errors)
|
|
1769
|
+
)
|
|
1770
|
+
};
|
|
1714
1771
|
},
|
|
1715
1772
|
async get(id) {
|
|
1716
1773
|
return expectAgentTask(
|
|
@@ -1733,6 +1790,16 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1733
1790
|
errors
|
|
1734
1791
|
);
|
|
1735
1792
|
},
|
|
1793
|
+
async setInstructions(id, instructions) {
|
|
1794
|
+
return expectAgentTask(
|
|
1795
|
+
await transport.request(`${path(id)}/instructions`, {
|
|
1796
|
+
method: "PUT",
|
|
1797
|
+
body: { instructions }
|
|
1798
|
+
}),
|
|
1799
|
+
"Agent task instructions response",
|
|
1800
|
+
errors
|
|
1801
|
+
);
|
|
1802
|
+
},
|
|
1736
1803
|
async archive(id) {
|
|
1737
1804
|
expectEmpty(
|
|
1738
1805
|
await transport.request(`${path(id)}/archive`, { method: "PATCH" }),
|
|
@@ -1777,6 +1844,26 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1777
1844
|
"Agent task channel response",
|
|
1778
1845
|
errors
|
|
1779
1846
|
);
|
|
1847
|
+
},
|
|
1848
|
+
async turnStates(ids) {
|
|
1849
|
+
const unique = [...new Set(ids.map((id) => id.toLowerCase()))].filter((id) => UUID.test(id));
|
|
1850
|
+
const states = [];
|
|
1851
|
+
for (let start = 0; start < unique.length; start += TURN_STATES_BATCH_SIZE) {
|
|
1852
|
+
const taskIds = unique.slice(start, start + TURN_STATES_BATCH_SIZE);
|
|
1853
|
+
states.push(
|
|
1854
|
+
...expectAgentTaskTurnStates(
|
|
1855
|
+
await transport.request("/api/v1/tasks/turn-states", {
|
|
1856
|
+
method: "GET",
|
|
1857
|
+
// Joined here because a transport may send an array as repeated keys; the Copilot
|
|
1858
|
+
// reads one comma-separated value.
|
|
1859
|
+
params: { taskIds: taskIds.join(",") }
|
|
1860
|
+
}),
|
|
1861
|
+
"Agent task turn states response",
|
|
1862
|
+
errors
|
|
1863
|
+
)
|
|
1864
|
+
);
|
|
1865
|
+
}
|
|
1866
|
+
return states;
|
|
1780
1867
|
}
|
|
1781
1868
|
};
|
|
1782
1869
|
}
|
|
@@ -3626,10 +3713,13 @@ function unavailableTransport(name, errors) {
|
|
|
3626
3713
|
// src/agentTurnError.ts
|
|
3627
3714
|
var AgentTaskTurnError = class extends Error {
|
|
3628
3715
|
code;
|
|
3629
|
-
|
|
3716
|
+
/** The compactions of a turn the box closed with an error; empty when it reported none. */
|
|
3717
|
+
compactions;
|
|
3718
|
+
constructor(message, code, compactions = []) {
|
|
3630
3719
|
super(message);
|
|
3631
3720
|
this.name = "AgentTaskTurnError";
|
|
3632
3721
|
this.code = code;
|
|
3722
|
+
this.compactions = compactions;
|
|
3633
3723
|
}
|
|
3634
3724
|
};
|
|
3635
3725
|
|
|
@@ -4781,6 +4871,7 @@ var CoreAgentTaskSession = class {
|
|
|
4781
4871
|
recoveryGeneration = 0;
|
|
4782
4872
|
recoveredTerminalReason;
|
|
4783
4873
|
recoveredTerminalUsage;
|
|
4874
|
+
recoveredTerminalCompactions;
|
|
4784
4875
|
// Polls that found no closing row although a terminal with usage was heard.
|
|
4785
4876
|
endedPollsWithoutRow = 0;
|
|
4786
4877
|
// The turn ids the running turn went by, and the highest box sequence seen in it. The Copilot
|
|
@@ -4897,7 +4988,7 @@ var CoreAgentTaskSession = class {
|
|
|
4897
4988
|
const error = new AgentTaskTurnError(
|
|
4898
4989
|
"Agent turn cancellation was not acknowledged before the safety timeout."
|
|
4899
4990
|
);
|
|
4900
|
-
this.emit("error", { error: error.message });
|
|
4991
|
+
this.emit("error", { error: error.message, compactions: [] });
|
|
4901
4992
|
this.failTurn(error);
|
|
4902
4993
|
}
|
|
4903
4994
|
}, CANCEL_SAFETY_MS);
|
|
@@ -5204,6 +5295,7 @@ var CoreAgentTaskSession = class {
|
|
|
5204
5295
|
this.recoveryUsed = false;
|
|
5205
5296
|
this.recoveredTerminalReason = void 0;
|
|
5206
5297
|
this.recoveredTerminalUsage = void 0;
|
|
5298
|
+
this.recoveredTerminalCompactions = void 0;
|
|
5207
5299
|
this.recoveryGeneration += 1;
|
|
5208
5300
|
this.activeWaiter = waiter;
|
|
5209
5301
|
this.setStatus("streaming");
|
|
@@ -5381,6 +5473,7 @@ var CoreAgentTaskSession = class {
|
|
|
5381
5473
|
this.recoveryUsed = false;
|
|
5382
5474
|
this.recoveredTerminalReason = void 0;
|
|
5383
5475
|
this.recoveredTerminalUsage = void 0;
|
|
5476
|
+
this.recoveredTerminalCompactions = void 0;
|
|
5384
5477
|
this.recoveryGeneration += 1;
|
|
5385
5478
|
this.turnBaselineIds = new Set(this._history.map((item) => item.id));
|
|
5386
5479
|
void this.captureTurnBaseline().catch(() => void 0);
|
|
@@ -5515,19 +5608,28 @@ var CoreAgentTaskSession = class {
|
|
|
5515
5608
|
);
|
|
5516
5609
|
if (!newest) return false;
|
|
5517
5610
|
this._content = newest.content;
|
|
5518
|
-
this.finishTurn(reason, this.recoveredTerminalUsage);
|
|
5611
|
+
this.finishTurn(reason, this.recoveredTerminalUsage, this.recoveredTerminalEnd());
|
|
5519
5612
|
return true;
|
|
5520
5613
|
}
|
|
5521
5614
|
if (recovered.type === "ERROR") {
|
|
5522
5615
|
const error = new AgentTaskTurnError(recovered.content);
|
|
5523
|
-
this.emit("error", { error: error.message });
|
|
5616
|
+
this.emit("error", { error: error.message, compactions: [] });
|
|
5524
5617
|
this.failTurn(error);
|
|
5525
5618
|
return true;
|
|
5526
5619
|
}
|
|
5527
5620
|
this._content = recovered.content;
|
|
5528
|
-
this.finishTurn(
|
|
5621
|
+
this.finishTurn(
|
|
5622
|
+
reason,
|
|
5623
|
+
recovered.usage ?? this.recoveredTerminalUsage,
|
|
5624
|
+
this.recoveredTerminalEnd()
|
|
5625
|
+
);
|
|
5529
5626
|
return true;
|
|
5530
5627
|
}
|
|
5628
|
+
// The history rows carry no compactions: only a terminal heard live has the turn's list.
|
|
5629
|
+
recoveredTerminalEnd() {
|
|
5630
|
+
const compactions = this.recoveredTerminalCompactions;
|
|
5631
|
+
return compactions ? { compactions } : {};
|
|
5632
|
+
}
|
|
5531
5633
|
closesThisTurn(message) {
|
|
5532
5634
|
if (!message.usage) return false;
|
|
5533
5635
|
const prompt = message.usage.requestMessageId;
|
|
@@ -5688,9 +5790,11 @@ var CoreAgentTaskSession = class {
|
|
|
5688
5790
|
const finished = typeof lifecycle2?.turnId === "string" ? lifecycle2.turnId : void 0;
|
|
5689
5791
|
if (finished !== void 0 && this.finishedTurnIds.includes(finished)) break;
|
|
5690
5792
|
if (finished === void 0 && lifecycle2 && this.turnIds.size > 0) break;
|
|
5793
|
+
const compactions = agentTurnCompactionsOf(payload?.compactions);
|
|
5691
5794
|
const end = {
|
|
5692
5795
|
...sequenceOf(event),
|
|
5693
|
-
...finished === void 0 ? {} : { turnId: finished }
|
|
5796
|
+
...finished === void 0 ? {} : { turnId: finished },
|
|
5797
|
+
compactions
|
|
5694
5798
|
};
|
|
5695
5799
|
if (reason === "interrupted" || lifecycle2?.interruptTerminal === true) {
|
|
5696
5800
|
this.rememberFinishedTurn(lifecycle2);
|
|
@@ -5702,6 +5806,7 @@ var CoreAgentTaskSession = class {
|
|
|
5702
5806
|
if (this.recoveryUsed) {
|
|
5703
5807
|
this.recoveredTerminalReason = reason;
|
|
5704
5808
|
this.recoveredTerminalUsage = usage;
|
|
5809
|
+
this.recoveredTerminalCompactions = compactions;
|
|
5705
5810
|
if (!this.recoveryPromise) {
|
|
5706
5811
|
const generation = this.recoveryGeneration;
|
|
5707
5812
|
this.recoveryPromise = this.recoverTurn(generation).finally(() => {
|
|
@@ -5717,24 +5822,20 @@ var CoreAgentTaskSession = class {
|
|
|
5717
5822
|
case "error": {
|
|
5718
5823
|
const code = typeof payload?.code === "string" ? payload.code : void 0;
|
|
5719
5824
|
const message = typeof payload?.message === "string" ? payload.message : "Agent returned an error.";
|
|
5720
|
-
|
|
5721
|
-
|
|
5722
|
-
|
|
5723
|
-
|
|
5724
|
-
|
|
5725
|
-
|
|
5726
|
-
|
|
5727
|
-
|
|
5728
|
-
|
|
5729
|
-
if (steered) steered.refusal = new AgentTaskTurnError(message, code);
|
|
5730
|
-
break;
|
|
5731
|
-
}
|
|
5732
|
-
} else if (!payload?.lifecycle && !this.dispatching && this.pendingSteers > 0) break;
|
|
5733
|
-
if (this._status === "streaming" || this._status === "cancelled") {
|
|
5734
|
-
this.failTurn(new AgentTaskTurnError(message, code));
|
|
5735
|
-
}
|
|
5825
|
+
const compactions = agentTurnCompactionsOf(payload?.compactions);
|
|
5826
|
+
const endsTurn = this.boxErrorEndsTurn(payload, message, code);
|
|
5827
|
+
this.emit("error", {
|
|
5828
|
+
...code ? { code } : {},
|
|
5829
|
+
error: message,
|
|
5830
|
+
...sequenceOf(event),
|
|
5831
|
+
...endsTurn ? { compactions } : {}
|
|
5832
|
+
});
|
|
5833
|
+
if (endsTurn) this.failTurn(new AgentTaskTurnError(message, code, compactions));
|
|
5736
5834
|
break;
|
|
5737
5835
|
}
|
|
5836
|
+
case "contextCompacted":
|
|
5837
|
+
if (!this.isFinishedTurnFrame(lifecycle)) this.emitCompaction(event, payload);
|
|
5838
|
+
break;
|
|
5738
5839
|
case "approval":
|
|
5739
5840
|
case "user_input_request":
|
|
5740
5841
|
this.liveRequest(event, payload);
|
|
@@ -5878,6 +5979,22 @@ var CoreAgentTaskSession = class {
|
|
|
5878
5979
|
this.turnIds.add(turnId);
|
|
5879
5980
|
return false;
|
|
5880
5981
|
}
|
|
5982
|
+
// A refused steer is handed its refusal here, for the send that waits on it.
|
|
5983
|
+
boxErrorEndsTurn(payload, message, code) {
|
|
5984
|
+
if (!payload?.lifecycle && payload?.requestId !== void 0) return false;
|
|
5985
|
+
if (typeof payload?.command === "string") return false;
|
|
5986
|
+
const refused = payload?.lifecycle ? void 0 : payload?.clientMessageId;
|
|
5987
|
+
if (typeof refused === "string") {
|
|
5988
|
+
if (refused !== this.openingMessageId) {
|
|
5989
|
+
const steered = [...this.steers, ...this.strayedSteers].find(
|
|
5990
|
+
(pending) => pending.clientMessageId === refused
|
|
5991
|
+
);
|
|
5992
|
+
if (steered) steered.refusal = new AgentTaskTurnError(message, code);
|
|
5993
|
+
return false;
|
|
5994
|
+
}
|
|
5995
|
+
} else if (!payload?.lifecycle && !this.dispatching && this.pendingSteers > 0) return false;
|
|
5996
|
+
return this._status === "streaming" || this._status === "cancelled";
|
|
5997
|
+
}
|
|
5881
5998
|
isFinishedTurnFrame(lifecycle) {
|
|
5882
5999
|
const turnId = lifecycle?.turnId;
|
|
5883
6000
|
return typeof turnId === "string" && this.finishedTurnIds.includes(turnId);
|
|
@@ -6105,10 +6222,22 @@ var CoreAgentTaskSession = class {
|
|
|
6105
6222
|
case "user_input_request":
|
|
6106
6223
|
this.liveRequest(event, payload, parentToolId);
|
|
6107
6224
|
break;
|
|
6225
|
+
case "contextCompacted":
|
|
6226
|
+
this.emitCompaction(event, payload, parentToolId);
|
|
6227
|
+
break;
|
|
6108
6228
|
default:
|
|
6109
6229
|
break;
|
|
6110
6230
|
}
|
|
6111
6231
|
}
|
|
6232
|
+
emitCompaction(event, payload, parentToolId) {
|
|
6233
|
+
const compaction = agentTurnCompactionOf(payload);
|
|
6234
|
+
if (!compaction) return;
|
|
6235
|
+
this.emit("contextCompacted", {
|
|
6236
|
+
...compaction,
|
|
6237
|
+
...pieceIdentityOf(event, payload),
|
|
6238
|
+
...parentToolIdOf(parentToolId)
|
|
6239
|
+
});
|
|
6240
|
+
}
|
|
6112
6241
|
applySubagent(event, payload) {
|
|
6113
6242
|
const subagentId = payload?.subagentId;
|
|
6114
6243
|
const toolId = payload?.toolId;
|
|
@@ -6172,7 +6301,8 @@ var CoreAgentTaskSession = class {
|
|
|
6172
6301
|
content: this._content,
|
|
6173
6302
|
reason,
|
|
6174
6303
|
...usage ? { usage } : {},
|
|
6175
|
-
...end
|
|
6304
|
+
...end,
|
|
6305
|
+
compactions: end.compactions ?? []
|
|
6176
6306
|
};
|
|
6177
6307
|
this.emit("turnEnd", result);
|
|
6178
6308
|
this.activeWaiter?.resolve(result);
|
|
@@ -6292,6 +6422,13 @@ var CoreAgentTaskSession = class {
|
|
|
6292
6422
|
}
|
|
6293
6423
|
};
|
|
6294
6424
|
|
|
6425
|
+
// src/agentTurnEvent.ts
|
|
6426
|
+
function isAgentTurnEvent(value) {
|
|
6427
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
6428
|
+
const event = value;
|
|
6429
|
+
return (event.type === "STARTED" || event.type === "ENDED") && typeof event.eventId === "string" && typeof event.taskId === "string" && typeof event.turnId === "string" && typeof event.occurredAt === "string";
|
|
6430
|
+
}
|
|
6431
|
+
|
|
6295
6432
|
// src/apiKeySession.ts
|
|
6296
6433
|
var API_KEY_EXCHANGE_PATH = "/api/v1/auth/exchange";
|
|
6297
6434
|
function appAccessTokenPath(appId) {
|
|
@@ -6402,6 +6539,7 @@ export {
|
|
|
6402
6539
|
expectObjectArray,
|
|
6403
6540
|
expectPage,
|
|
6404
6541
|
expectStringArray,
|
|
6542
|
+
isAgentTurnEvent,
|
|
6405
6543
|
isWorkspaceSession,
|
|
6406
6544
|
readTokenAppId,
|
|
6407
6545
|
readTokenExpiry,
|