@arbidocs/sdk 0.3.16 → 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
  *
@@ -652,6 +661,27 @@ async function streamSSE(response, callbacks = {}) {
652
661
  context
653
662
  };
654
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
+ }
655
685
  var consumeSSEStream = streamSSE;
656
686
  var AUTH_TIMEOUT_MS = 1e4;
657
687
  var MAX_BACKOFF_MS = 3e4;
@@ -876,6 +906,66 @@ function formatUserName(user) {
876
906
  return [user.given_name, user.family_name].filter(Boolean).join(" ");
877
907
  }
878
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
+
879
969
  // src/operations/documents.ts
880
970
  var documents_exports = {};
881
971
  __export(documents_exports, {
@@ -1573,6 +1663,19 @@ var Arbi = class {
1573
1663
  lr.serverSessionKey,
1574
1664
  signingPrivateKeyBase64
1575
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
+ }
1576
1679
  this.currentWorkspaceId = ws.external_id;
1577
1680
  }
1578
1681
  /** Log out and clear internal state. */
@@ -1991,6 +2094,7 @@ exports.connectWithReconnect = connectWithReconnect;
1991
2094
  exports.consumeSSEStream = consumeSSEStream;
1992
2095
  exports.contacts = contacts_exports;
1993
2096
  exports.conversations = conversations_exports;
2097
+ exports.countCitations = countCitations;
1994
2098
  exports.createAuthenticatedClient = createAuthenticatedClient;
1995
2099
  exports.createDocumentWaiter = createDocumentWaiter;
1996
2100
  exports.dm = dm_exports;
@@ -2000,6 +2104,7 @@ exports.documentsNode = documents_node_exports;
2000
2104
  exports.files = files_exports;
2001
2105
  exports.formatAgentStepLabel = formatAgentStepLabel;
2002
2106
  exports.formatFileSize = formatFileSize;
2107
+ exports.formatStreamSummary = formatStreamSummary;
2003
2108
  exports.formatUserName = formatUserName;
2004
2109
  exports.formatWorkspaceChoices = formatWorkspaceChoices;
2005
2110
  exports.formatWsMessage = formatWsMessage;
@@ -2012,12 +2117,15 @@ exports.performPasswordLogin = performPasswordLogin;
2012
2117
  exports.requireData = requireData;
2013
2118
  exports.requireOk = requireOk;
2014
2119
  exports.resolveAuth = resolveAuth;
2120
+ exports.resolveCitations = resolveCitations;
2015
2121
  exports.resolveWorkspace = resolveWorkspace;
2016
2122
  exports.responses = responses_exports;
2017
2123
  exports.selectWorkspace = selectWorkspace;
2018
2124
  exports.selectWorkspaceById = selectWorkspaceById;
2019
2125
  exports.settings = settings_exports;
2020
2126
  exports.streamSSE = streamSSE;
2127
+ exports.stripCitationMarkdown = stripCitationMarkdown;
2128
+ exports.summarizeCitations = summarizeCitations;
2021
2129
  exports.tags = tags_exports;
2022
2130
  exports.workspaces = workspaces_exports;
2023
2131
  //# sourceMappingURL=index.cjs.map