@agentknight/sdk 0.9.61 → 0.9.62

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.
Files changed (2) hide show
  1. package/dist/index.js +23 -13
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10649,30 +10649,28 @@ function createResearchContextReader(options) {
10649
10649
  throw new Error("The source did not return its current plan");
10650
10650
  }
10651
10651
  return {
10652
- id: plan.id,
10653
- turnId: plan.turnId,
10654
- operation: plan.operation,
10655
- content: plan.content,
10656
- todos: plan.todos
10652
+ anchorEventId: plan.sourceEventIds.at(-1),
10653
+ plan: { id: plan.id, turnId: plan.turnId, operation: plan.operation, content: plan.content, todos: plan.todos }
10657
10654
  };
10658
10655
  } catch (error) {
10659
10656
  throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE", "The current plan could not be read", { cause: error });
10660
10657
  }
10661
10658
  };
10662
- const currentPlan = await readPlan();
10659
+ const { plan: currentPlan, anchorEventId: planAnchor } = await readPlan();
10663
10660
  let page = await options.readTimeline(sessionId);
10664
10661
  if (page.cursorStatus === "expired") throw new ResearchError("RESEARCH_CONTEXT_CHANGED");
10665
10662
  const headIdentity = JSON.stringify(page);
10666
10663
  let selectedCursor;
10667
- for (let count = 1; sourceItemId !== void 0 && !page.items.some((item) => item.id === sourceItemId) && page.hasMore && count < MAX_ANCHOR_PAGES; count += 1) {
10664
+ for (let count = 1; (sourceItemId === void 0 ? page.items.length === 0 : !page.items.some((item) => item.id === sourceItemId)) && page.hasMore && count < MAX_ANCHOR_PAGES; count += 1) {
10668
10665
  selectedCursor = page.cursor;
10669
10666
  page = await options.readTimeline(sessionId, { cursor: selectedCursor });
10670
10667
  if (page.cursorStatus === "expired") throw new ResearchError("RESEARCH_CONTEXT_CHANGED");
10671
10668
  }
10672
10669
  const index = sourceItemId === void 0 ? page.items.length - 1 : page.items.findIndex((item) => item.id === sourceItemId);
10673
- if (index < 0) throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE", "Refresh the conversation before starting research");
10670
+ const emptyHistory = sourceItemId === void 0 && page.items.length === 0 && !page.hasMore;
10671
+ if (index < 0 && !emptyHistory) throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE");
10674
10672
  const items = page.items.slice(0, index + 1);
10675
- const anchor = items.at(-1)?.sourceEventIds.at(-1);
10673
+ const anchor = emptyHistory ? planAnchor : items.at(-1)?.sourceEventIds.at(-1);
10676
10674
  if (anchor === void 0) throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE");
10677
10675
  const units = [];
10678
10676
  for (const item of items) {
@@ -10686,6 +10684,7 @@ function createResearchContextReader(options) {
10686
10684
  }
10687
10685
  const selected = [];
10688
10686
  let length = JSON.stringify({ conversation: [], currentPlan }).length;
10687
+ if (length > MAX_CONTEXT_LENGTH) throw new ResearchError("RESEARCH_CONTEXT_TOO_LARGE");
10689
10688
  for (const unit of [...units].reverse()) {
10690
10689
  const text = JSON.stringify(unit.map(contextItem));
10691
10690
  if (length + text.length + (selected.length > 0 ? 1 : 0) > MAX_CONTEXT_LENGTH) {
@@ -10696,8 +10695,8 @@ function createResearchContextReader(options) {
10696
10695
  selected.unshift(text);
10697
10696
  length += text.length + (selected.length > 1 ? 1 : 0);
10698
10697
  }
10699
- if (selected.length === 0) throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE");
10700
- if (JSON.stringify(await readPlan()) !== JSON.stringify(currentPlan)) {
10698
+ if (selected.length === 0 && !emptyHistory) throw new ResearchError("RESEARCH_CONTEXT_UNAVAILABLE");
10699
+ if (JSON.stringify((await readPlan()).plan) !== JSON.stringify(currentPlan)) {
10701
10700
  throw new ResearchError("RESEARCH_CONTEXT_CHANGED");
10702
10701
  }
10703
10702
  if (selectedCursor !== void 0 && JSON.stringify(await options.readTimeline(sessionId, { cursor: selectedCursor })) !== JSON.stringify(page)) {
@@ -11827,7 +11826,7 @@ var preconditionMessages = {
11827
11826
  RESEARCH_SOURCE_MISMATCH: "The source context does not match this session",
11828
11827
  RESEARCH_SOURCE_INVALID: "Choose a main session as the research source",
11829
11828
  RESEARCH_CONTEXT_CHANGED: "The source conversation changed; refresh it before starting research",
11830
- RESEARCH_CONTEXT_UNAVAILABLE: "Refresh the conversation before starting research",
11829
+ RESEARCH_CONTEXT_UNAVAILABLE: "The source conversation could not be read. Try sending again.",
11831
11830
  RESEARCH_CONTEXT_TOO_LARGE: "The selected conversation unit is too large for research; choose another message",
11832
11831
  RESEARCH_OPERATION_CONFLICT: "The operation ID was reused with different research data",
11833
11832
  RESEARCH_THREAD_LIMIT: "The research thread limit has been reached",
@@ -11862,7 +11861,18 @@ function createResearchWorkbench(options) {
11862
11861
  insights: (sessionId) => researchOperation(() => sessions.insights(sessionId)),
11863
11862
  create: (input) => researchOperation(async () => {
11864
11863
  const assertAccess = options.captureSourceAccess?.(input.parentSessionId);
11865
- const snapshot = await options.contextSnapshot(input.parentSessionId, input.sourceItemId);
11864
+ let snapshot;
11865
+ for (let attempt = 0; ; attempt++) {
11866
+ assertAccess?.();
11867
+ try {
11868
+ snapshot = await options.contextSnapshot(input.parentSessionId, input.sourceItemId);
11869
+ break;
11870
+ } catch (error) {
11871
+ if (!(error instanceof ResearchError) || input.sourceItemId !== void 0 || attempt >= 2 || !["RESEARCH_CONTEXT_CHANGED", "RESEARCH_CONTEXT_UNAVAILABLE"].includes(error.code)) throw error;
11872
+ options.onError?.(error);
11873
+ await new Promise((resolve5) => setTimeout(resolve5, 250 * (attempt + 1)));
11874
+ }
11875
+ }
11866
11876
  assertAccess?.();
11867
11877
  return projectThread(await sessions.create({
11868
11878
  operationId: input.operationId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentknight/sdk",
3
- "version": "0.9.61",
3
+ "version": "0.9.62",
4
4
  "description": "AgentKnight runtime SDK for local and remote coding sessions",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",