@arbidocs/sdk 0.3.15 → 0.3.17

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
@@ -129,11 +129,13 @@ var FileConfigStore = class {
129
129
  configFile;
130
130
  credentialsFile;
131
131
  sessionFile;
132
+ metadataFile;
132
133
  constructor(configDir) {
133
134
  this.configDir = configDir ?? process.env.ARBI_CONFIG_DIR ?? path2__default.default.join(os__default.default.homedir(), ".arbi");
134
135
  this.configFile = path2__default.default.join(this.configDir, "config.json");
135
136
  this.credentialsFile = path2__default.default.join(this.configDir, "credentials.json");
136
137
  this.sessionFile = path2__default.default.join(this.configDir, "session.json");
138
+ this.metadataFile = path2__default.default.join(this.configDir, "last-metadata.json");
137
139
  }
138
140
  ensureConfigDir() {
139
141
  if (!fs__default.default.existsSync(this.configDir)) {
@@ -204,6 +206,13 @@ var FileConfigStore = class {
204
206
  clearChatSession() {
205
207
  this.saveChatSession({ ...DEFAULT_SESSION });
206
208
  }
209
+ // ── Last metadata (for citation browsing) ────────────────────────────────
210
+ saveLastMetadata(metadata) {
211
+ this.writeSecureFile(this.metadataFile, metadata);
212
+ }
213
+ loadLastMetadata() {
214
+ return this.readJsonFile(this.metadataFile);
215
+ }
207
216
  /**
208
217
  * Try to resolve config from multiple sources, in priority order:
209
218
  *
@@ -577,6 +586,14 @@ async function streamSSE(response, callbacks = {}) {
577
586
  callbacks.onError?.(message);
578
587
  },
579
588
  // ARBI-specific events (dot-prefixed from server)
589
+ "arbi.message_queued": (raw) => {
590
+ const data = JSON.parse(raw);
591
+ if (data.user_message) {
592
+ userMessage = data.user_message;
593
+ callbacks.onUserMessage?.(data.user_message);
594
+ }
595
+ callbacks.onMessageQueued?.(data);
596
+ },
580
597
  "arbi.agent_step": (raw) => {
581
598
  const data = JSON.parse(raw);
582
599
  const label = formatAgentStepLabel(data);
@@ -644,6 +661,27 @@ async function streamSSE(response, callbacks = {}) {
644
661
  context
645
662
  };
646
663
  }
664
+ function formatStreamSummary(result, elapsedTime) {
665
+ const parts = [];
666
+ if (result.agentSteps.length > 0) {
667
+ let stepLabel = `${result.agentSteps.length} step${result.agentSteps.length === 1 ? "" : "s"}`;
668
+ if (result.toolCallCount > 0) {
669
+ stepLabel += ` (${result.toolCallCount} tool call${result.toolCallCount === 1 ? "" : "s"})`;
670
+ }
671
+ parts.push(stepLabel);
672
+ }
673
+ if (result.usage) {
674
+ parts.push(`${result.usage.total_tokens.toLocaleString()} tokens`);
675
+ }
676
+ if (result.context && result.context.context_window > 0) {
677
+ const used = result.context.all_llm_calls?.last_input_tokens ?? result.context.total_input;
678
+ parts.push(`${used.toLocaleString()}/${result.context.context_window.toLocaleString()} context`);
679
+ }
680
+ if (elapsedTime != null) {
681
+ parts.push(`${elapsedTime.toFixed(1)}s`);
682
+ }
683
+ return parts.length > 0 ? parts.join(" \xB7 ") : "";
684
+ }
647
685
  var consumeSSEStream = streamSSE;
648
686
  var AUTH_TIMEOUT_MS = 1e4;
649
687
  var MAX_BACKOFF_MS = 3e4;
@@ -868,6 +906,66 @@ function formatUserName(user) {
868
906
  return [user.given_name, user.family_name].filter(Boolean).join(" ");
869
907
  }
870
908
 
909
+ // src/citations.ts
910
+ function resolveCitations(metadata) {
911
+ if (!metadata?.tools) return [];
912
+ const tools = metadata.tools;
913
+ const modelCitations = tools.model_citations;
914
+ const citationMap = modelCitations?.tool_responses;
915
+ if (!citationMap || Object.keys(citationMap).length === 0) return [];
916
+ const chunkLookup = buildChunkLookup(tools);
917
+ const resolved = [];
918
+ for (const [citationNum, citationData] of Object.entries(citationMap)) {
919
+ const chunks = [];
920
+ for (const chunkId of citationData.chunk_ids ?? []) {
921
+ const chunk = chunkLookup.get(chunkId);
922
+ if (chunk) chunks.push(chunk);
923
+ }
924
+ resolved.push({ citationNum, citationData, chunks });
925
+ }
926
+ resolved.sort((a, b) => Number(a.citationNum) - Number(b.citationNum));
927
+ return resolved;
928
+ }
929
+ function summarizeCitations(resolved) {
930
+ return resolved.map((r) => {
931
+ const firstChunk = r.chunks[0];
932
+ return {
933
+ citationNum: r.citationNum,
934
+ statement: r.citationData.statement ?? "",
935
+ docTitle: firstChunk?.metadata?.doc_title ?? "Unknown document",
936
+ pageNumber: firstChunk?.metadata?.page_number ?? null,
937
+ chunkCount: r.chunks.length
938
+ };
939
+ });
940
+ }
941
+ function countCitations(metadata) {
942
+ if (!metadata?.tools) return 0;
943
+ const tools = metadata.tools;
944
+ const modelCitations = tools.model_citations;
945
+ const responses = modelCitations?.tool_responses;
946
+ return responses ? Object.keys(responses).length : 0;
947
+ }
948
+ function stripCitationMarkdown(text) {
949
+ return text.replace(/\[([^\]]+)\]\(#cite-(\d+)\)/g, "$1[$2]");
950
+ }
951
+ function buildChunkLookup(tools) {
952
+ const lookup = /* @__PURE__ */ new Map();
953
+ for (const toolName of ["retrieval_chunk", "retrieval_full_context"]) {
954
+ const tool = tools[toolName];
955
+ if (!tool?.tool_responses) continue;
956
+ for (const chunks of Object.values(tool.tool_responses)) {
957
+ if (!Array.isArray(chunks)) continue;
958
+ for (const chunk of chunks) {
959
+ const id = chunk.metadata?.chunk_ext_id;
960
+ if (id && !lookup.has(id)) {
961
+ lookup.set(id, chunk);
962
+ }
963
+ }
964
+ }
965
+ }
966
+ return lookup;
967
+ }
968
+
871
969
  // src/operations/documents.ts
872
970
  var documents_exports = {};
873
971
  __export(documents_exports, {
@@ -1565,6 +1663,19 @@ var Arbi = class {
1565
1663
  lr.serverSessionKey,
1566
1664
  signingPrivateKeyBase64
1567
1665
  );
1666
+ const workspaceKeyHeader = client.session.getWorkspaceKeyHeader();
1667
+ if (workspaceKeyHeader) {
1668
+ const { data: openResult, error: openError } = await client.fetch.POST(
1669
+ "/v1/workspace/{workspace_ext_id}/open",
1670
+ {
1671
+ params: { path: { workspace_ext_id: ws.external_id } },
1672
+ body: { workspace_key: workspaceKeyHeader }
1673
+ }
1674
+ );
1675
+ if (!openError && openResult?.access_token) {
1676
+ client.session.setAccessToken(openResult.access_token);
1677
+ }
1678
+ }
1568
1679
  this.currentWorkspaceId = ws.external_id;
1569
1680
  }
1570
1681
  /** Log out and clear internal state. */
@@ -1983,6 +2094,7 @@ exports.connectWithReconnect = connectWithReconnect;
1983
2094
  exports.consumeSSEStream = consumeSSEStream;
1984
2095
  exports.contacts = contacts_exports;
1985
2096
  exports.conversations = conversations_exports;
2097
+ exports.countCitations = countCitations;
1986
2098
  exports.createAuthenticatedClient = createAuthenticatedClient;
1987
2099
  exports.createDocumentWaiter = createDocumentWaiter;
1988
2100
  exports.dm = dm_exports;
@@ -1992,6 +2104,7 @@ exports.documentsNode = documents_node_exports;
1992
2104
  exports.files = files_exports;
1993
2105
  exports.formatAgentStepLabel = formatAgentStepLabel;
1994
2106
  exports.formatFileSize = formatFileSize;
2107
+ exports.formatStreamSummary = formatStreamSummary;
1995
2108
  exports.formatUserName = formatUserName;
1996
2109
  exports.formatWorkspaceChoices = formatWorkspaceChoices;
1997
2110
  exports.formatWsMessage = formatWsMessage;
@@ -2004,12 +2117,15 @@ exports.performPasswordLogin = performPasswordLogin;
2004
2117
  exports.requireData = requireData;
2005
2118
  exports.requireOk = requireOk;
2006
2119
  exports.resolveAuth = resolveAuth;
2120
+ exports.resolveCitations = resolveCitations;
2007
2121
  exports.resolveWorkspace = resolveWorkspace;
2008
2122
  exports.responses = responses_exports;
2009
2123
  exports.selectWorkspace = selectWorkspace;
2010
2124
  exports.selectWorkspaceById = selectWorkspaceById;
2011
2125
  exports.settings = settings_exports;
2012
2126
  exports.streamSSE = streamSSE;
2127
+ exports.stripCitationMarkdown = stripCitationMarkdown;
2128
+ exports.summarizeCitations = summarizeCitations;
2013
2129
  exports.tags = tags_exports;
2014
2130
  exports.workspaces = workspaces_exports;
2015
2131
  //# sourceMappingURL=index.cjs.map