@thinkingai/ae-cli 6.1.24 → 6.1.26

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 (195) hide show
  1. package/README.md +35 -10
  2. package/README.zh.md +32 -10
  3. package/bin/ae-cli.js +6 -1
  4. package/cli-installation-guide.md +91 -0
  5. package/dist/{auth-XZNXFRJS.js → auth-YHMC3GDH.js} +168 -10
  6. package/dist/automation-NWWVLWG2.js +459 -0
  7. package/dist/{capability-FYTU3X6L.js → capability-3QLLIVFB.js} +11 -6
  8. package/dist/chunk-4655PUNX.js +43 -0
  9. package/dist/{chunk-4NRCTUZJ.js → chunk-5XJYLEVC.js} +4 -4
  10. package/dist/{chunk-JOCDD4ON.js → chunk-7QXISROD.js} +1 -1
  11. package/dist/{chunk-EQ255HKO.js → chunk-AP7YP2NX.js} +2 -2
  12. package/dist/{chunk-DVMLWQD4.js → chunk-CT7QDJS6.js} +2 -2
  13. package/dist/chunk-D6ACFILC.js +56 -0
  14. package/dist/{chunk-HFGZGYCG.js → chunk-EOYSKMXN.js} +264 -10
  15. package/dist/{chunk-57RRFUEZ.js → chunk-FMNNGNYO.js} +43 -8
  16. package/dist/{chunk-7XGFSUOM.js → chunk-GIK6EDUG.js} +291 -84
  17. package/dist/chunk-HVHR4SCP.js +133 -0
  18. package/dist/{chunk-CCS66K4K.js → chunk-KAEHZZH4.js} +3 -3
  19. package/dist/{chunk-DX5CDO34.js → chunk-KFNMQTZI.js} +580 -214
  20. package/dist/{chunk-HBGADCKA.js → chunk-PLE722AJ.js} +28 -11
  21. package/dist/chunk-RK6GLDGI.js +27 -0
  22. package/dist/{chunk-V2NUHPXF.js → chunk-S6M42KND.js} +1 -1
  23. package/dist/{chunk-7FTTULED.js → chunk-UQ77JXYH.js} +7 -3
  24. package/dist/{chunk-MAPZ7VB6.js → chunk-WNGFSIOB.js} +20 -1
  25. package/dist/{chunk-VYXXOKYS.js → chunk-XCHAYMSF.js} +7 -4
  26. package/dist/{community-report-client-CU22W53K.js → community-report-client-PEERQCX2.js} +1 -1
  27. package/dist/{config-XO3LI3SA.js → config-AHPMEW5A.js} +25 -6
  28. package/dist/context-YAE3QXP3.js +136 -0
  29. package/dist/{data-integration-HNGUKFIC.js → data-integration-HESZET7E.js} +6 -6
  30. package/dist/index.js +167 -53
  31. package/dist/{local-data-upload-client-GTRIT64F.js → local-data-upload-client-TWMHFLUA.js} +1 -1
  32. package/dist/memory-62BJOULV.js +580 -0
  33. package/dist/{metadata-DI5BMYFU.js → metadata-C7CEDHOJ.js} +7 -7
  34. package/dist/{model-RNPQXIBI.js → model-SBV2JFJI.js} +9 -5
  35. package/dist/{personal-semantic-preference-5C3WUXFI.js → personal-semantic-preference-QECDPHND.js} +119 -11
  36. package/dist/project-semantic-Z4QAMBWF.js +356 -0
  37. package/dist/{sync-O45NXEQY.js → sync-SU33KYX7.js} +12 -8
  38. package/dist/{te-agent-K5MSPNHJ.js → te-agent-F5QBZSQA.js} +423 -494
  39. package/dist/{te-analysis-AISWCOOZ.js → te-analysis-2ECAAIFK.js} +1227 -154
  40. package/dist/{te-community-4OYU3BDA.js → te-community-PFLDLPP4.js} +3 -3
  41. package/dist/{te-dataops-NFIPB6FX.js → te-dataops-FOQSRIAZ.js} +644 -276
  42. package/dist/{te-engage-BAM3GBFX.js → te-engage-QKUGIWSR.js} +6 -6
  43. package/dist/{te-experiment-GUTZXZ5Z.js → te-experiment-26PXR2UO.js} +6 -6
  44. package/dist/{te-kb-3TS73NO6.js → te-kb-MHDOST62.js} +666 -244
  45. package/dist/{te-system-STMHLS5J.js → te-system-J2FQF5ER.js} +6 -6
  46. package/dist/{te-team-3I5AOX3N.js → te-team-5UOZPTU3.js} +6 -6
  47. package/dist/things-VPPXXLHB.js +1091 -0
  48. package/dist/{update-RZXLPCRH.js → update-EEUTEI6R.js} +59 -51
  49. package/dist/workspace-O3EEIYQM.js +554 -0
  50. package/package.json +30 -11
  51. package/skills/ae-agent/SKILL.md +95 -140
  52. package/skills/ae-agent/references/add-model.md +1 -1
  53. package/skills/ae-agent/references/create-agent.md +1 -1
  54. package/skills/ae-agent/references/find-archived-conversations.md +12 -5
  55. package/skills/ae-agent/references/get-agent.md +1 -1
  56. package/skills/ae-agent/references/list-agents.md +2 -2
  57. package/skills/ae-agent/references/list-models.md +2 -2
  58. package/skills/ae-agent/references/notification.md +40 -0
  59. package/skills/ae-agent/references/restore-conversation.md +3 -1
  60. package/skills/ae-analysis/SKILL.md +55 -23
  61. package/skills/ae-analysis/references/adhoc_export.md +1 -1
  62. package/skills/ae-analysis/references/adhoc_run.md +1 -1
  63. package/skills/ae-analysis/references/agent_review_submit_to_page.md +2 -0
  64. package/skills/ae-analysis/references/ai_models/event.md +6 -1
  65. package/skills/ae-analysis/references/ai_models/funnel.md +12 -4
  66. package/skills/ae-analysis/references/ai_models/heat_map.md +23 -0
  67. package/skills/ae-analysis/references/ai_models/interval.md +24 -0
  68. package/skills/ae-analysis/references/ai_models/path.md +20 -0
  69. package/skills/ae-analysis/references/ai_models/prop_analysis.md +2 -0
  70. package/skills/ae-analysis/references/ai_models/rank_list.md +30 -0
  71. package/skills/ae-analysis/references/ai_models/retention.md +45 -1
  72. package/skills/ae-analysis/references/ai_models/revenue.md +20 -2
  73. package/skills/ae-analysis/references/ai_models/session.md +76 -0
  74. package/skills/ae-analysis/references/ai_models/tag.md +2 -0
  75. package/skills/ae-analysis/references/ai_models.md +100 -3
  76. package/skills/ae-analysis/references/analysis_data_retrieval.md +4 -0
  77. package/skills/ae-analysis/references/analysis_drilldown_contract.md +4 -1
  78. package/skills/ae-analysis/references/asset_batch_info_export.md +10 -0
  79. package/skills/ae-analysis/references/asset_batch_sql_export.md +13 -1
  80. package/skills/ae-analysis/references/asset_export.md +12 -1
  81. package/skills/ae-analysis/references/asset_list.md +1 -0
  82. package/skills/ae-analysis/references/asset_search.md +11 -5
  83. package/skills/ae-analysis/references/bi_panel_list.md +1 -1
  84. package/skills/ae-analysis/references/catalog_list.md +1 -1
  85. package/skills/ae-analysis/references/collaboration.md +48 -0
  86. package/skills/ae-analysis/references/command_index.md +29 -19
  87. package/skills/ae-analysis/references/dashboard_list.md +4 -4
  88. package/skills/ae-analysis/references/dashboard_move.md +21 -0
  89. package/skills/ae-analysis/references/dashboard_report_add.md +2 -0
  90. package/skills/ae-analysis/references/dashboard_report_data_export.md +2 -0
  91. package/skills/ae-analysis/references/dashboard_report_get.md +19 -0
  92. package/skills/ae-analysis/references/dashboard_report_update.md +27 -0
  93. package/skills/ae-analysis/references/dashboard_update.md +1 -1
  94. package/skills/ae-analysis/references/drilldown_session_details_run.md +41 -0
  95. package/skills/ae-analysis/references/event_export.md +3 -1
  96. package/skills/ae-analysis/references/event_report_presentation.md +76 -0
  97. package/skills/ae-analysis/references/feedback_submit.md +37 -0
  98. package/skills/ae-analysis/references/governance_recommendation_auto_review.md +77 -0
  99. package/skills/ae-analysis/references/governance_recommendation_export.md +11 -0
  100. package/skills/ae-analysis/references/metadata_resolution.md +18 -3
  101. package/skills/ae-analysis/references/metric_export.md +3 -1
  102. package/skills/ae-analysis/references/metric_list.md +1 -1
  103. package/skills/ae-analysis/references/operation_record_export.md +10 -0
  104. package/skills/ae-analysis/references/personal_semantic_preference_add.md +2 -2
  105. package/skills/ae-analysis/references/personal_semantic_preference_get.md +4 -4
  106. package/skills/ae-analysis/references/personal_semantic_preference_list.md +4 -4
  107. package/skills/ae-analysis/references/personal_semantic_preference_review.md +9 -0
  108. package/skills/ae-analysis/references/personal_semantic_preference_review_list.md +7 -0
  109. package/skills/ae-analysis/references/personal_semantic_preference_submit.md +7 -0
  110. package/skills/ae-analysis/references/project_semantic_knowledge_wiki.md +91 -32
  111. package/skills/ae-analysis/references/project_semantic_knowledge_wiki_plan_schema.md +99 -1
  112. package/skills/ae-analysis/references/property_export.md +3 -1
  113. package/skills/ae-analysis/references/report_create.md +18 -4
  114. package/skills/ae-analysis/references/report_data_export.md +6 -0
  115. package/skills/ae-analysis/references/report_get.md +7 -1
  116. package/skills/ae-analysis/references/report_list.md +5 -5
  117. package/skills/ae-analysis/references/report_update.md +21 -4
  118. package/skills/ae-analysis/references/sql_table_columns.md +4 -4
  119. package/skills/ae-analysis/references/sql_table_list.md +5 -5
  120. package/skills/ae-analysis/references/system_query_monitor_overview.md +2 -0
  121. package/skills/ae-analysis/references/system_query_task_cancel.md +2 -0
  122. package/skills/ae-analysis/references/system_query_task_export.md +6 -2
  123. package/skills/ae-analysis/references/system_query_task_get.md +2 -0
  124. package/skills/ae-analysis/references/system_query_task_list.md +7 -3
  125. package/skills/ae-analysis/references/system_query_task_options.md +2 -2
  126. package/skills/ae-analysis/references/system_query_task_realtime_list.md +15 -0
  127. package/skills/ae-analysis/references/user_cluster_models.md +8 -0
  128. package/skills/ae-analysis/references/user_tag_create.md +7 -1
  129. package/skills/ae-analysis/references/user_tag_get.md +1 -1
  130. package/skills/ae-analysis/references/user_tag_models.md +20 -2
  131. package/skills/ae-analysis/references/user_tag_refresh.md +1 -1
  132. package/skills/ae-analysis/references/user_tag_update.md +2 -2
  133. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/build-empty-skeleton.mjs +105 -0
  134. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/build-project-semantic-wiki.mjs +669 -29
  135. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/company-kb.mjs +21 -8
  136. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/default-compile-rules.md +28 -10
  137. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/default-compile-schema.md +66 -0
  138. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/generate-build-ir.mjs +285 -24
  139. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/package-wiki-source-zip.mjs +262 -30
  140. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/plan-kb-source-sync.mjs +1 -1
  141. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/precompiled-source.mjs +46 -2
  142. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/prepare-compile-schema.mjs +64 -0
  143. package/skills/ae-analysis/scripts/project-semantic-knowledge-wiki/refresh-state-parts.mjs +91 -0
  144. package/skills/ae-automation/SKILL.md +16 -0
  145. package/skills/ae-automation/references/cli.md +53 -0
  146. package/skills/ae-automation/references/command_index.md +17 -0
  147. package/skills/ae-capability/SKILL.md +73 -2
  148. package/skills/ae-capability/references/collaboration.md +48 -0
  149. package/skills/ae-community/SKILL.md +5 -1
  150. package/skills/ae-community/references/collaboration.md +48 -0
  151. package/skills/ae-current-context/SKILL.md +3 -2
  152. package/skills/ae-data-integration/SKILL.md +4 -0
  153. package/skills/ae-data-integration/references/collaboration.md +48 -0
  154. package/skills/ae-dataops/SKILL.md +54 -74
  155. package/skills/ae-dataops/references/collaboration.md +48 -0
  156. package/skills/ae-dataops/references/dataops-backfill.md +23 -18
  157. package/skills/ae-dataops/references/dataops-flow-create.md +74 -23
  158. package/skills/ae-dataops/references/dataops-flow-monitor.md +45 -13
  159. package/skills/ae-dataops/references/dataops-integration.md +57 -30
  160. package/skills/ae-dataops/references/dataops-query.md +21 -4
  161. package/skills/ae-dataops/references/dataops-table.md +143 -11
  162. package/skills/ae-engage/SKILL.md +21 -6
  163. package/skills/ae-engage/references/channel-mgmt.md +3 -3
  164. package/skills/ae-engage/references/collaboration.md +48 -0
  165. package/skills/ae-engage/references/scene-config-channel.md +4 -4
  166. package/skills/ae-generate-tracking-plan/SKILL.md +4 -0
  167. package/skills/ae-generate-tracking-plan/references/collaboration.md +48 -0
  168. package/skills/ae-kb/SKILL.md +70 -12
  169. package/skills/ae-kb/references/analysis-workflow.md +73 -36
  170. package/skills/ae-kb/references/collaboration.md +48 -0
  171. package/skills/ae-kb/references/query-workflow.md +67 -33
  172. package/skills/ae-kb/references/schema-import.md +32 -0
  173. package/skills/ae-kb/references/source-mutations.md +6 -4
  174. package/skills/ae-kb/references/text-sources.md +53 -0
  175. package/skills/ae-kb-discovery/SKILL.md +8 -3
  176. package/skills/ae-kb-discovery/references/collaboration.md +48 -0
  177. package/skills/ae-metadata/SKILL.md +20 -12
  178. package/skills/ae-metadata/references/collaboration.md +48 -0
  179. package/skills/ae-team/SKILL.md +23 -17
  180. package/skills/ae-things/SKILL.md +85 -0
  181. package/skills/ae-things/references/cli.md +103 -0
  182. package/skills/ae-things-progress/SKILL.md +43 -0
  183. package/skills/ae-things-review/SKILL.md +42 -0
  184. package/skills/ae-workspace/SKILL.md +43 -0
  185. package/skills/ae-workspace/references/commands.md +51 -0
  186. package/dist/chunk-E2JKXMVO.js +0 -29
  187. package/dist/context-IFJI3LIT.js +0 -46
  188. package/dist/memory-QGPSNCQS.js +0 -892
  189. package/dist/project-semantic-3LF6Q6JZ.js +0 -1119
  190. package/skills/ae-agent/references/create-automation.md +0 -125
  191. package/skills/ae-agent/references/list-automations.md +0 -56
  192. package/skills/ae-agent/references/update-automation.md +0 -107
  193. package/skills/ae-project-semantic/SKILL.md +0 -193
  194. package/skills/ae-project-semantic/references/query-routing-v5.md +0 -165
  195. package/skills/ae-project-semantic/references/recommendation-quality.md +0 -68
@@ -4,14 +4,259 @@ import {
4
4
  } from "./chunk-Y74WTIKJ.js";
5
5
  import {
6
6
  SecureStoreAuthError,
7
+ clearCliTokenCache,
7
8
  getCliToken
8
- } from "./chunk-7XGFSUOM.js";
9
+ } from "./chunk-GIK6EDUG.js";
9
10
  import {
10
11
  safeJsonParse
11
- } from "./chunk-7FTTULED.js";
12
+ } from "./chunk-UQ77JXYH.js";
12
13
  import {
13
14
  PermissionError
14
- } from "./chunk-MAPZ7VB6.js";
15
+ } from "./chunk-WNGFSIOB.js";
16
+
17
+ // src/core/cli-agent-context.ts
18
+ import { readFileSync, statSync } from "fs";
19
+ import { dirname, join, parse, resolve } from "path";
20
+ import { AsyncLocalStorage } from "async_hooks";
21
+ var MAX_CONTEXT_FILE_BYTES = 64 * 1024;
22
+ var CONTEXT_FILE_ENV = "TA_CLI_CONTEXT_FILE";
23
+ var CONTEXT_ARG = "--agent-context";
24
+ var CONTEXT_ENCODING_HEADER = "X-TA-CLI-Context-Encoding";
25
+ var CONTEXT_HEADER_ENCODING = "base64url";
26
+ var WELL_KNOWN_CONTEXT_FILES = [
27
+ ".ta/cli-agent-context.json",
28
+ ".ae/cli-agent-context.json"
29
+ ];
30
+ var FIELDS = [
31
+ { key: "agent_client", env: "TA_CLI_AGENT_CLIENT", header: "X-TA-CLI-Agent-Client", maxLength: 64 },
32
+ { key: "agent_model", env: "TA_CLI_AGENT_MODEL", header: "X-TA-CLI-Agent-Model", maxLength: 128 },
33
+ { key: "agent_session_id", env: "TA_CLI_AGENT_SESSION_ID", header: "X-TA-CLI-Agent-Session-Id", maxLength: 191 },
34
+ { key: "agent_turn_id", env: "TA_CLI_AGENT_TURN_ID", header: "X-TA-CLI-Agent-Turn-Id", maxLength: 191 },
35
+ { key: "parent_turn_id", env: "TA_CLI_PARENT_TURN_ID", header: "X-TA-CLI-Parent-Turn-Id", maxLength: 191 },
36
+ { key: "intent_relation", env: "TA_CLI_INTENT_RELATION", header: "X-TA-CLI-Intent-Relation", maxLength: 64 },
37
+ { key: "intent_revision", env: "TA_CLI_INTENT_REVISION", header: "X-TA-CLI-Intent-Revision", maxLength: 64 },
38
+ { key: "runtime", env: "TA_CLI_RUNTIME", header: "X-TA-CLI-Runtime", maxLength: 64 },
39
+ { key: "intent_source", env: "TA_CLI_INTENT_SOURCE", header: "X-TA-CLI-Intent-Source", maxLength: 64 },
40
+ { key: "session_initial_intent", env: "TA_CLI_SESSION_INITIAL_INTENT", header: "X-TA-CLI-Session-Initial-Intent", maxLength: 2048 },
41
+ { key: "user_intent", env: "TA_CLI_USER_INTENT", header: "X-TA-CLI-User-Intent", maxLength: 2048 },
42
+ { key: "user_intent_hash", env: "TA_CLI_USER_INTENT_HASH", header: "X-TA-CLI-User-Intent-Hash", maxLength: 128 },
43
+ { key: "session_goal", env: "TA_CLI_SESSION_GOAL", header: "X-TA-CLI-Session-Goal", maxLength: 2048 },
44
+ { key: "context_capture_status", env: "TA_CLI_CONTEXT_CAPTURE_STATUS", header: "X-TA-CLI-Context-Capture-Status", maxLength: 32 }
45
+ ];
46
+ var scopedContext = new AsyncLocalStorage();
47
+ function runWithCliAgentContext(context, run) {
48
+ return scopedContext.run(context, run);
49
+ }
50
+ function readCliAgentContext() {
51
+ const context = {
52
+ ...inferRuntimeContext(),
53
+ ...readEnvContext(),
54
+ ...readArgumentContext(),
55
+ ...readFileContext(),
56
+ ...scopedContext.getStore()
57
+ };
58
+ return withCaptureStatus(context);
59
+ }
60
+ function cliAgentContextHeaders(context = readCliAgentContext()) {
61
+ const headers = {};
62
+ for (const field of FIELDS) {
63
+ if (!field.header) continue;
64
+ const value = context[field.key];
65
+ if (value) headers[field.header] = Buffer.from(value, "utf8").toString("base64url");
66
+ }
67
+ if (Object.keys(headers).length > 0) {
68
+ headers[CONTEXT_ENCODING_HEADER] = CONTEXT_HEADER_ENCODING;
69
+ }
70
+ return headers;
71
+ }
72
+ function withCliAgentContextBody(body, context = readCliAgentContext()) {
73
+ if (!hasContext(context) || !isPlainRecord(body)) {
74
+ return body;
75
+ }
76
+ return {
77
+ ...body,
78
+ client_context: context
79
+ };
80
+ }
81
+ function readEnvContext() {
82
+ const context = {};
83
+ for (const field of FIELDS) {
84
+ const value = cleanValue(process.env[field.env], field.maxLength);
85
+ if (value) context[field.key] = value;
86
+ }
87
+ return context;
88
+ }
89
+ function inferRuntimeContext() {
90
+ return inferCodexContext() ?? inferClaudeCodeContext() ?? inferWorkBuddyContext() ?? {};
91
+ }
92
+ function inferCodexContext() {
93
+ if (!process.env.CODEX_SESSION_ID && !process.env.CODEX_THREAD_ID && !process.env.CODEX_VERSION && !process.env.CODEX_SHELL) {
94
+ return void 0;
95
+ }
96
+ return compactContext({
97
+ agent_client: "codex",
98
+ agent_model: firstEnv("CODEX_MODEL", "OPENAI_MODEL"),
99
+ agent_session_id: firstEnv("CODEX_THREAD_ID", "CODEX_SESSION_ID"),
100
+ runtime: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE ? "codex_desktop" : "codex_cli",
101
+ intent_source: "runtime_env"
102
+ });
103
+ }
104
+ function inferClaudeCodeContext() {
105
+ if (!firstEnv("CLAUDE_CODE_SESSION_ID", "CLAUDE_SESSION_ID", "CLAUDECODE", "CLAUDE_CODE")) {
106
+ return void 0;
107
+ }
108
+ return compactContext({
109
+ agent_client: "claude_code",
110
+ agent_model: firstEnv("CLAUDE_CODE_MODEL", "CLAUDE_MODEL", "ANTHROPIC_MODEL"),
111
+ agent_session_id: firstEnv("CLAUDE_CODE_SESSION_ID", "CLAUDE_SESSION_ID"),
112
+ runtime: "claude_code",
113
+ intent_source: "runtime_env"
114
+ });
115
+ }
116
+ function inferWorkBuddyContext() {
117
+ if (!firstEnv("WORKBUDDY_SESSION_ID", "WORKBUDDY_AGENT_ID", "WORKBUDDY_RUNTIME")) {
118
+ return void 0;
119
+ }
120
+ return compactContext({
121
+ agent_client: "workbuddy",
122
+ agent_model: firstEnv("WORKBUDDY_MODEL", "WORKBUDDY_AGENT_MODEL"),
123
+ agent_session_id: firstEnv("WORKBUDDY_SESSION_ID", "WORKBUDDY_CONVERSATION_ID"),
124
+ runtime: firstEnv("WORKBUDDY_RUNTIME") ?? "workbuddy",
125
+ intent_source: "runtime_env"
126
+ });
127
+ }
128
+ function compactContext(values) {
129
+ const context = {};
130
+ for (const field of FIELDS) {
131
+ const value = cleanValue(values[field.key], field.maxLength);
132
+ if (value) context[field.key] = value;
133
+ }
134
+ return context;
135
+ }
136
+ function firstEnv(...keys) {
137
+ for (const key of keys) {
138
+ const value = process.env[key]?.trim();
139
+ if (value) return value;
140
+ }
141
+ return void 0;
142
+ }
143
+ function readFileContext() {
144
+ const filePath = resolveContextFilePath();
145
+ if (!filePath) return {};
146
+ try {
147
+ const stat = statSync(filePath);
148
+ if (!stat.isFile() || stat.size > MAX_CONTEXT_FILE_BYTES) return {};
149
+ const parsed = JSON.parse(readFileSync(filePath, "utf8"));
150
+ const record = normalizeContextFileRecord(parsed);
151
+ if (!record) return {};
152
+ const context = {};
153
+ for (const field of FIELDS) {
154
+ const value = cleanValue(record[field.key], field.maxLength);
155
+ if (value) context[field.key] = value;
156
+ }
157
+ return context;
158
+ } catch {
159
+ return {};
160
+ }
161
+ }
162
+ function readArgumentContext() {
163
+ const raw = readContextArgument(process.argv.slice(2));
164
+ if (!raw) return {};
165
+ const parsed = parseContextArgument(raw);
166
+ if (!parsed) return {};
167
+ const context = contextFromRecord(parsed);
168
+ if (!hasContext(context)) return {};
169
+ return {
170
+ intent_source: "agent_argument",
171
+ ...context
172
+ };
173
+ }
174
+ function readContextArgument(args) {
175
+ for (let i = 0; i < args.length; i += 1) {
176
+ const token = args[i];
177
+ if (token === "--") return void 0;
178
+ if (token === CONTEXT_ARG) return args[i + 1];
179
+ if (token.startsWith(`${CONTEXT_ARG}=`)) return token.slice(CONTEXT_ARG.length + 1);
180
+ }
181
+ return void 0;
182
+ }
183
+ function parseContextArgument(raw) {
184
+ const text = raw.trim();
185
+ if (!text) return void 0;
186
+ const decoded = text.startsWith("{") ? text : decodeBase64Url(text);
187
+ if (!decoded) return void 0;
188
+ try {
189
+ const parsed = JSON.parse(decoded);
190
+ return normalizeContextFileRecord(parsed);
191
+ } catch {
192
+ return void 0;
193
+ }
194
+ }
195
+ function decodeBase64Url(value) {
196
+ try {
197
+ return Buffer.from(value, "base64url").toString("utf8");
198
+ } catch {
199
+ return void 0;
200
+ }
201
+ }
202
+ function resolveContextFilePath() {
203
+ const configured = process.env[CONTEXT_FILE_ENV]?.trim();
204
+ if (configured) return configured;
205
+ return findWellKnownContextFile(process.cwd());
206
+ }
207
+ function findWellKnownContextFile(startDir) {
208
+ let current = resolve(startDir);
209
+ const root = parse(current).root;
210
+ while (true) {
211
+ for (const fileName of WELL_KNOWN_CONTEXT_FILES) {
212
+ const candidate = join(current, fileName);
213
+ try {
214
+ const stat = statSync(candidate);
215
+ if (stat.isFile() && stat.size <= MAX_CONTEXT_FILE_BYTES) return candidate;
216
+ } catch {
217
+ }
218
+ }
219
+ if (current === root) return void 0;
220
+ current = dirname(current);
221
+ }
222
+ }
223
+ function normalizeContextFileRecord(value) {
224
+ if (!isPlainRecord(value)) return void 0;
225
+ if (isPlainRecord(value.client_context)) return value.client_context;
226
+ return value;
227
+ }
228
+ function contextFromRecord(record) {
229
+ const context = {};
230
+ for (const field of FIELDS) {
231
+ const value = cleanValue(record[field.key], field.maxLength);
232
+ if (value) context[field.key] = value;
233
+ }
234
+ return context;
235
+ }
236
+ function withCaptureStatus(context) {
237
+ if (!hasContext(context)) return {};
238
+ if (context.context_capture_status) return context;
239
+ const required = ["agent_client", "agent_session_id", "agent_turn_id", "user_intent"];
240
+ const complete = required.every((key) => Boolean(context[key]));
241
+ return {
242
+ ...context,
243
+ context_capture_status: complete ? "full" : "partial"
244
+ };
245
+ }
246
+ function hasContext(context) {
247
+ return Object.keys(context).some((key) => key !== "context_capture_status" && Boolean(context[key]));
248
+ }
249
+ function cleanValue(value, maxLength) {
250
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
251
+ return void 0;
252
+ }
253
+ const text = String(value).replace(/[\r\n\t]+/g, " ").trim();
254
+ if (!text) return void 0;
255
+ return text.length > maxLength ? text.slice(0, maxLength) : text;
256
+ }
257
+ function isPlainRecord(value) {
258
+ return value !== null && typeof value === "object" && !Array.isArray(value);
259
+ }
15
260
 
16
261
  // src/core/capability-api.ts
17
262
  import { readFile } from "fs/promises";
@@ -32,11 +277,11 @@ var CapabilityGatewayError = class extends Error {
32
277
  meta;
33
278
  type;
34
279
  };
35
- function buildCapabilityGatewayUrl(host, domain, pathAfterV1, queryParams = {}) {
280
+ function buildCapabilityGatewayUrl(host, domain, pathAfterV1, queryParams = {}, apiVersion = "v1") {
36
281
  const base = host.replace(/\/+$/, "");
37
282
  const cleanDomain = domain.replace(/^\/+|\/+$/g, "");
38
283
  const cleanPath = pathAfterV1.replace(/^\/+|\/+$/g, "");
39
- const gatewayPrefix = cleanDomain === "" ? "/api/cli/v1" : `/api/cli/${cleanDomain}/v1`;
284
+ const gatewayPrefix = cleanDomain === "" ? `/api/cli/${apiVersion}` : `/api/cli/${cleanDomain}/${apiVersion}`;
40
285
  const url = new URL(`${base}${gatewayPrefix}/${cleanPath}`);
41
286
  for (const [key, value] of Object.entries(queryParams)) {
42
287
  if (value !== void 0 && value !== null) {
@@ -144,16 +389,18 @@ async function throwCapabilityHttpError(resp) {
144
389
  }
145
390
  throw error;
146
391
  }
147
- async function requestOnce(url, method, token, body, accept = "application/json") {
392
+ async function requestOnce(url, method, token, body, accept = "application/json", includeClientContextInBody = true) {
393
+ const agentContext = readCliAgentContext();
148
394
  const headers = {
149
395
  "cli-token": token,
150
396
  "Accept": accept,
151
- ...internalCallSourceHeaders()
397
+ ...internalCallSourceHeaders(),
398
+ ...cliAgentContextHeaders(agentContext)
152
399
  };
153
400
  const init = { method, headers };
154
401
  if (body !== void 0 && method !== "GET") {
155
402
  headers["Content-Type"] = "application/json";
156
- init.body = JSON.stringify(body);
403
+ init.body = JSON.stringify(includeClientContextInBody ? withCliAgentContextBody(body, agentContext) : body);
157
404
  }
158
405
  return fetch(url, init);
159
406
  }
@@ -172,9 +419,14 @@ async function callGateway(host, domain, pathAfterV1, method, body, queryParams
172
419
  return (await callGatewayWithEnvelope(host, domain, pathAfterV1, method, body, queryParams, options)).data;
173
420
  }
174
421
  async function callGatewayWithEnvelope(host, domain, pathAfterV1, method, body, queryParams = {}, options = {}) {
175
- const url = buildCapabilityGatewayUrl(options.apiBaseUrl ?? host, domain, pathAfterV1, queryParams);
422
+ const url = buildCapabilityGatewayUrl(options.apiBaseUrl ?? host, domain, pathAfterV1, queryParams, options.apiVersion);
176
423
  const token = await getCliToken(host);
177
- const resp = await requestOnce(url, method, token, body);
424
+ let resp = await requestOnce(url, method, token, body, "application/json", options.includeClientContextInBody);
425
+ if (resp.status === 401 && options.retryOnUnauthorized === true) {
426
+ await resp.body?.cancel();
427
+ clearCliTokenCache(host);
428
+ resp = await requestOnce(url, method, await getCliToken(host), body, "application/json", options.includeClientContextInBody);
429
+ }
178
430
  if (resp.status === 403) {
179
431
  const error = await permissionError(resp);
180
432
  if (options.retryOnInvalidTokenForbidden !== false && isInvalidCliTokenMessage(error.message)) {
@@ -303,6 +555,8 @@ async function callCapabilityApi(host, domain, action, method = "POST", params =
303
555
  }
304
556
 
305
557
  export {
558
+ runWithCliAgentContext,
559
+ readCliAgentContext,
306
560
  CapabilityGatewayError,
307
561
  buildCapabilityGatewayUrl,
308
562
  fetchCapabilityGateway,
@@ -1,17 +1,17 @@
1
1
  import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);
2
2
  import {
3
3
  CapabilityGatewayError
4
- } from "./chunk-HFGZGYCG.js";
4
+ } from "./chunk-EOYSKMXN.js";
5
5
  import {
6
6
  SecureStoreAuthError,
7
7
  getCliToken
8
- } from "./chunk-7XGFSUOM.js";
8
+ } from "./chunk-GIK6EDUG.js";
9
9
  import {
10
10
  safeJsonParse
11
- } from "./chunk-7FTTULED.js";
11
+ } from "./chunk-UQ77JXYH.js";
12
12
  import {
13
13
  PermissionError
14
- } from "./chunk-MAPZ7VB6.js";
14
+ } from "./chunk-WNGFSIOB.js";
15
15
 
16
16
  // src/core/mcp-access.ts
17
17
  function buildUrl(host, path, params = {}) {
@@ -26,6 +26,11 @@ function buildUrl(host, path, params = {}) {
26
26
  return url.toString();
27
27
  }
28
28
  var KbUnauthorizedError = class extends Error {
29
+ constructor(message, code) {
30
+ super(message);
31
+ this.code = code;
32
+ }
33
+ code;
29
34
  };
30
35
  function optionalString(value) {
31
36
  return typeof value === "string" && value.trim() ? value.trim() : void 0;
@@ -49,6 +54,16 @@ function responseErrorMessage(data) {
49
54
  const nestedError = data?.error && typeof data.error === "object" ? data.error : void 0;
50
55
  return optionalString(typeof data?.error === "string" ? data.error : void 0) ?? optionalString(nestedError?.message) ?? optionalString(data?.message) ?? optionalString(data?.return_message);
51
56
  }
57
+ function safeTextErrorCode(data) {
58
+ const rawCode = responseErrorMetadata(data).code;
59
+ return rawCode && /^[a-z0-9][a-z0-9_.:-]{0,127}$/i.test(rawCode) ? rawCode : void 0;
60
+ }
61
+ function safeTextError(resp, data) {
62
+ const code = safeTextErrorCode(data);
63
+ const message = resp.status === 404 && !code ? "This host does not support the KB text API (HTTP 404)." : `KB text request failed (HTTP ${resp.status}${code ? `, ${code}` : ""}).`;
64
+ const hint = code === "SOURCE_CONTENT_REVISION_CONFLICT" ? "The source changed. Keep your local input, read the current revision and explicitly resolve the conflict." : resp.status === 404 ? "Verify the exact source and host support for text APIs; no upload fallback was attempted." : "Inspect the source list and current state before retrying. Source content is omitted from diagnostics.";
65
+ return resp.status === 403 ? new PermissionError(message, code, hint) : new KbHttpError(message, code, resp.status, hint);
66
+ }
52
67
  function responseStatus(resp) {
53
68
  const statusText = optionalString(resp.statusText);
54
69
  return statusText ? `${resp.status} ${statusText}` : String(resp.status);
@@ -68,6 +83,7 @@ var KbHttpError = class extends CapabilityGatewayError {
68
83
  }
69
84
  };
70
85
  function nonOkResponseError(resp, data, options) {
86
+ if (options.redactErrorDetails) return safeTextError(resp, data);
71
87
  const metadata = responseErrorMetadata(data);
72
88
  const serverMessage = responseErrorMessage(data);
73
89
  const fallbackMessage = `KB API HTTP error: ${responseStatus(resp)}`;
@@ -84,41 +100,59 @@ function parseKbResponse(resp, text, options = {}) {
84
100
  let metadata = {};
85
101
  try {
86
102
  const d = safeJsonParse(text);
103
+ if (options.redactErrorDetails) throw safeTextError(resp, d);
87
104
  const m = d && (typeof d.error === "string" ? d.error : d.error?.message || d.message);
88
105
  if (m && typeof m === "string") msg = m;
89
106
  if (options.preserveErrorMetadata) metadata = responseErrorMetadata(d);
90
- } catch {
107
+ } catch (error) {
108
+ if (error instanceof PermissionError) throw error;
91
109
  }
92
110
  throw new PermissionError(msg, metadata.code, metadata.hint);
93
111
  }
94
112
  if (resp.status === 401) {
113
+ let code;
114
+ if (options.redactErrorDetails) {
115
+ try {
116
+ code = safeTextErrorCode(safeJsonParse(text));
117
+ } catch {
118
+ }
119
+ }
95
120
  throw new KbUnauthorizedError(
96
- `KB API token auth failed: HTTP ${resp.status} ${resp.statusText}`
121
+ options.redactErrorDetails ? "KB API token auth failed: HTTP 401." : `KB API token auth failed: HTTP ${resp.status} ${resp.statusText}`,
122
+ code
97
123
  );
98
124
  }
99
125
  if (!text.trim()) {
126
+ if (options.redactErrorDetails && !resp.ok) throw safeTextError(resp, void 0);
100
127
  throw malformedResponseError(resp, "empty response body");
101
128
  }
102
129
  let data;
103
130
  try {
104
131
  data = safeJsonParse(text);
105
132
  } catch {
133
+ if (options.redactErrorDetails && !resp.ok) throw safeTextError(resp, void 0);
106
134
  throw malformedResponseError(resp, "non-JSON response");
107
135
  }
108
136
  if (data?.return_code === -1001) {
109
137
  throw new KbUnauthorizedError(
110
- `KB API token auth failed: ${data.return_message || "unauthorized"} (code: ${data.return_code})`
138
+ options.redactErrorDetails ? "KB API token auth failed (code: -1001)." : `KB API token auth failed: ${data.return_message || "unauthorized"} (code: ${data.return_code})`,
139
+ options.redactErrorDetails ? safeTextErrorCode(data) : void 0
111
140
  );
112
141
  }
113
142
  if (!resp.ok) {
114
143
  throw nonOkResponseError(resp, data, options);
115
144
  }
116
145
  if (data?.return_code !== 0 && data?.return_code !== void 0) {
146
+ if (options.redactErrorDetails) throw safeTextError(resp, data);
117
147
  throw new Error(
118
148
  `AE API error: ${data.return_message || "unknown"} (code: ${data.return_code})`
119
149
  );
120
150
  }
151
+ if (options.redactErrorDetails && data?.error && typeof data.error === "object") {
152
+ throw safeTextError(resp, data);
153
+ }
121
154
  if (data && typeof data === "object" && typeof data.error === "string" && data.error.trim() && data.return_code === void 0 && data.success === void 0) {
155
+ if (options.redactErrorDetails) throw safeTextError(resp, data);
122
156
  if (options.preserveErrorMetadata) {
123
157
  const metadata = responseErrorMetadata(data);
124
158
  throw new CapabilityGatewayError(
@@ -171,7 +205,8 @@ async function requestWithCliToken(host, input, init, options) {
171
205
  }
172
206
  }
173
207
  throw new SecureStoreAuthError(
174
- `The stored CLI token for ${host} is invalid or expired. Run: ae-cli auth login --host ${host}`
208
+ `The stored CLI token for ${host} is invalid or expired. Run: ae-cli auth login --host ${host}`,
209
+ options.redactErrorDetails ? error.code : void 0
175
210
  );
176
211
  }
177
212
  throw error;