@downcity/agent 1.1.317 → 1.1.320

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 (75) hide show
  1. package/bin/executor/types/SessionHistoryMeta.d.ts +2 -0
  2. package/bin/executor/types/SessionHistoryMeta.d.ts.map +1 -1
  3. package/bin/index.d.ts +1 -1
  4. package/bin/index.d.ts.map +1 -1
  5. package/bin/index.js.map +1 -1
  6. package/bin/remote/RemoteSession.d.ts +2 -2
  7. package/bin/remote/RemoteSession.d.ts.map +1 -1
  8. package/bin/remote/RemoteSession.js +2 -2
  9. package/bin/remote/RemoteSession.js.map +1 -1
  10. package/bin/remote/RemoteTransport.d.ts +2 -2
  11. package/bin/remote/RemoteTransport.d.ts.map +1 -1
  12. package/bin/remote/transports/HttpRemoteAgentTransport.d.ts +2 -2
  13. package/bin/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
  14. package/bin/remote/transports/HttpRemoteAgentTransport.js +2 -2
  15. package/bin/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
  16. package/bin/remote/transports/RpcClient.d.ts +2 -2
  17. package/bin/remote/transports/RpcClient.d.ts.map +1 -1
  18. package/bin/remote/transports/RpcClient.js +2 -2
  19. package/bin/remote/transports/RpcClient.js.map +1 -1
  20. package/bin/remote/transports/RpcRemoteAgentTransport.d.ts +2 -2
  21. package/bin/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
  22. package/bin/remote/transports/RpcRemoteAgentTransport.js +2 -2
  23. package/bin/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
  24. package/bin/session/Session.d.ts +4 -4
  25. package/bin/session/Session.d.ts.map +1 -1
  26. package/bin/session/Session.js +47 -27
  27. package/bin/session/Session.js.map +1 -1
  28. package/bin/session/SessionLoop.d.ts.map +1 -1
  29. package/bin/session/SessionLoop.js +4 -2
  30. package/bin/session/SessionLoop.js.map +1 -1
  31. package/bin/session/SessionMessages.d.ts +7 -2
  32. package/bin/session/SessionMessages.d.ts.map +1 -1
  33. package/bin/session/SessionMessages.js +21 -13
  34. package/bin/session/SessionMessages.js.map +1 -1
  35. package/bin/session/SessionState.d.ts +13 -1
  36. package/bin/session/SessionState.d.ts.map +1 -1
  37. package/bin/session/SessionState.js +28 -4
  38. package/bin/session/SessionState.js.map +1 -1
  39. package/bin/session/storage/Metadata.d.ts.map +1 -1
  40. package/bin/session/storage/Metadata.js +7 -0
  41. package/bin/session/storage/Metadata.js.map +1 -1
  42. package/bin/types/agent/SessionActor.d.ts +3 -3
  43. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  44. package/bin/types/agent/SessionTypes.d.ts +20 -0
  45. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  46. package/bin/types/rpc/RpcProtocol.d.ts +2 -1
  47. package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
  48. package/bin/types/session/SessionCommand.d.ts +2 -0
  49. package/bin/types/session/SessionCommand.d.ts.map +1 -1
  50. package/bin/types/session/SessionLocalState.d.ts +3 -0
  51. package/bin/types/session/SessionLocalState.d.ts.map +1 -1
  52. package/bin/types/session/SessionMessages.d.ts +2 -0
  53. package/bin/types/session/SessionMessages.d.ts.map +1 -1
  54. package/package.json +6 -6
  55. package/scripts/remote-session-reconnect.test.mjs +25 -0
  56. package/scripts/session-config-turn-boundary.test.mjs +185 -0
  57. package/src/executor/types/SessionHistoryMeta.ts +2 -0
  58. package/src/index.ts +1 -0
  59. package/src/remote/RemoteSession.ts +6 -2
  60. package/src/remote/RemoteTransport.ts +6 -1
  61. package/src/remote/transports/HttpRemoteAgentTransport.ts +3 -1
  62. package/src/remote/transports/RpcClient.ts +3 -1
  63. package/src/remote/transports/RpcRemoteAgentTransport.ts +3 -1
  64. package/src/session/Session.ts +55 -28
  65. package/src/session/SessionLoop.ts +4 -1
  66. package/src/session/SessionMessages.ts +44 -13
  67. package/src/session/SessionState.ts +39 -5
  68. package/src/session/storage/Metadata.ts +10 -0
  69. package/src/types/agent/SessionActor.ts +3 -2
  70. package/src/types/agent/SessionTypes.ts +22 -0
  71. package/src/types/rpc/RpcProtocol.ts +6 -1
  72. package/src/types/session/SessionCommand.ts +2 -0
  73. package/src/types/session/SessionLocalState.ts +4 -0
  74. package/src/types/session/SessionMessages.ts +2 -0
  75. package/tsconfig.tsbuildinfo +1 -1
@@ -25,6 +25,15 @@ import { to_executor_history } from "@/session/messages/SessionMessageCodec.js";
25
25
  import type { SessionMessage } from "@/types/session/SessionMessage.js";
26
26
  import type { SessionStateOptions } from "@/types/session/SessionState.js";
27
27
  import type { SessionStore } from "@/types/store/SessionStore.js";
28
+ import type { SessionApprovalMode } from "@/types/session/SessionInteraction.js";
29
+
30
+ /** Session 模型配置写入结果。 */
31
+ export interface SessionModelSetResult {
32
+ /** 已包含最新运行时模型实例的 configured 快照。 */
33
+ config: AgentSessionConfigSnapshot;
34
+ /** 持久化模型身份是否发生真实变化。 */
35
+ changed: boolean;
36
+ }
28
37
 
29
38
  /**
30
39
  * 本地 Session 配置与 Metadata 状态管理器。
@@ -61,6 +70,11 @@ export class SessionState {
61
70
  };
62
71
  }
63
72
 
73
+ /** 读取当前 Session 已接受的 Shell 审批模式。 */
74
+ get_approval_mode(): SessionApprovalMode {
75
+ return this.state.configured_approval_mode;
76
+ }
77
+
64
78
  /**
65
79
  * 读取当前 session 创建时间。
66
80
  */
@@ -99,10 +113,13 @@ export class SessionState {
99
113
  });
100
114
  this.state.created_at = created_at;
101
115
  this.state.timezone = timezone;
102
- this.state.session_config = {};
116
+ this.state.session_config = {
117
+ ...(metadata.model_label ? { model_label: metadata.model_label } : {}),
118
+ };
103
119
  this.state.effective_session_config = {
104
120
  ...this.state.session_config,
105
121
  };
122
+ this.state.configured_approval_mode = metadata.approval_mode || "ask";
106
123
  })();
107
124
  await this.state.initialize_promise;
108
125
  }
@@ -141,23 +158,40 @@ export class SessionState {
141
158
  /**
142
159
  * 写入当前 session 配置。
143
160
  */
144
- async set_model(model: AgentModel): Promise<AgentSessionConfigSnapshot> {
161
+ async set_model(model: AgentModel): Promise<SessionModelSetResult> {
145
162
  const next_model_label = infer_agent_model_label(model);
163
+ const changed = next_model_label !== this.state.session_config.model_label;
146
164
  const next_config: AgentSessionConfigSnapshot = {
147
165
  ...this.state.session_config,
148
166
  model,
149
167
  model_label: next_model_label,
150
168
  model_context_window: read_agent_model_context_window(model),
151
169
  };
170
+ if (changed) {
171
+ const metadata = await this.store.read_metadata();
172
+ await this.store.write_metadata({
173
+ ...metadata,
174
+ agent_id: this.agent_id,
175
+ updated_at: Date.now(),
176
+ ...(next_model_label ? { model_label: next_model_label } : {}),
177
+ });
178
+ }
179
+ this.state.session_config = next_config;
180
+ return { config: next_config, changed };
181
+ }
182
+
183
+ /** 接受并持久化当前 Session 的 Shell 审批模式。 */
184
+ async set_approval_mode(mode: SessionApprovalMode): Promise<boolean> {
185
+ if (mode === this.state.configured_approval_mode) return false;
152
186
  const metadata = await this.store.read_metadata();
153
187
  await this.store.write_metadata({
154
188
  ...metadata,
155
189
  agent_id: this.agent_id,
156
190
  updated_at: Date.now(),
157
- ...(next_model_label ? { model_label: next_model_label } : {}),
191
+ approval_mode: mode,
158
192
  });
159
- this.state.session_config = next_config;
160
- return next_config;
193
+ this.state.configured_approval_mode = mode;
194
+ return true;
161
195
  }
162
196
 
163
197
  /** 在 Session Step 检查点提交模型配置。 */
@@ -14,6 +14,13 @@ function normalizeModelLabel(input: unknown): string | undefined {
14
14
  return label || undefined;
15
15
  }
16
16
 
17
+ /** 归一化 Session Shell 审批模式。 */
18
+ function normalize_approval_mode(
19
+ input: unknown,
20
+ ): "ask" | "always-allow" | undefined {
21
+ return input === "ask" || input === "always-allow" ? input : undefined;
22
+ }
23
+
17
24
  /**
18
25
  * 归一化 session 标题。
19
26
  */
@@ -106,6 +113,9 @@ export function normalize_session_metadata(
106
113
  ...(normalizeModelLabel(raw.model_label)
107
114
  ? { model_label: normalizeModelLabel(raw.model_label) }
108
115
  : {}),
116
+ ...(normalize_approval_mode(raw.approval_mode)
117
+ ? { approval_mode: normalize_approval_mode(raw.approval_mode) }
118
+ : {}),
109
119
  ...(normalize_message_count(raw.message_count) !== undefined
110
120
  ? { message_count: normalize_message_count(raw.message_count) }
111
121
  : {}),
@@ -18,6 +18,7 @@ import type {
18
18
  AgentSessionForkInput,
19
19
  AgentSessionInfo,
20
20
  AgentSessionSetInput,
21
+ AgentSessionSetOptions,
21
22
  AgentSessionStatus,
22
23
  AgentSessionSummaryPage,
23
24
  AgentSessionSystemSnapshot,
@@ -121,7 +122,7 @@ export interface AgentSession extends AgentSessionActor {
121
122
  readonly config: AgentSessionConfigSnapshot;
122
123
 
123
124
  /** 写入当前 session 默认配置。 */
124
- set(input: AgentSessionSetInput): Promise<void>;
125
+ set(input: AgentSessionSetInput, options?: AgentSessionSetOptions): Promise<void>;
125
126
 
126
127
  /** 把当前 Session 首次生成后固定的完整 system 显式固化到 instruction.md。 */
127
128
  snapshot(): Promise<void>;
@@ -138,7 +139,7 @@ export interface AgentSession extends AgentSessionActor {
138
139
  */
139
140
  export interface RemoteAgentSession extends AgentSessionActor {
140
141
  /** 写入当前远程 Session 的可序列化动态配置。 */
141
- set(input: RemoteSessionSetInput): Promise<void>;
142
+ set(input: RemoteSessionSetInput, options?: AgentSessionSetOptions): Promise<void>;
142
143
 
143
144
  /** 从当前远程 session 创建一个分叉会话。 */
144
145
  fork(input?: AgentSessionForkInput | string): Promise<RemoteAgentSession>;
@@ -80,6 +80,28 @@ export interface AgentSessionSetInput {
80
80
  security?: AgentSessionSecurityConfig;
81
81
  }
82
82
 
83
+ /** Session 配置提交后的 Action 可观测性选项。 */
84
+ export interface AgentSessionSetOptions {
85
+ /**
86
+ * 是否把真实配置变化持久化为 completed Action Message。
87
+ *
88
+ * 关键点(中文)
89
+ * - 默认值为 true。
90
+ * - 设为 false 只关闭 Action Message,不影响配置写入与检查点提交。
91
+ * - 适用于 Session 初始化、恢复与其他不应进入时间线的宿主装配流程。
92
+ */
93
+ persist_action?: boolean;
94
+
95
+ /**
96
+ * 是否在 Action Message 持久化成功后发布对应 Session Mutation。
97
+ *
98
+ * 关键点(中文)
99
+ * - 默认跟随 `persist_action`。
100
+ * - 只有 `persist_action=true` 时才允许设为 true,避免发布不存在于 canonical Message 历史中的事件。
101
+ */
102
+ publish_mutation?: boolean;
103
+ }
104
+
83
105
  /** 可通过本地或远程 Session 动态更新的安全策略。 */
84
106
  export interface AgentSessionSecurityConfig {
85
107
  /** 高风险操作是否需要用户逐次审批。 */
@@ -15,6 +15,7 @@ import type {
15
15
  AgentArchiveSessionResult,
16
16
  AgentArchiveSessionsResult,
17
17
  AgentCleanArchiveResult,
18
+ AgentSessionSetOptions,
18
19
  RemoteSessionSetInput,
19
20
  AgentSessionSystemSnapshot,
20
21
  } from "@/types/agent/SessionTypes.js";
@@ -155,7 +156,11 @@ export type RpcRequest =
155
156
  /** 更新指定 Session 的可序列化动态配置。 */
156
157
  method: "sdk.sessions.set";
157
158
  /** Session 与动态配置参数。 */
158
- params: { session_id: string; input: RemoteSessionSetInput };
159
+ params: {
160
+ session_id: string;
161
+ input: RemoteSessionSetInput;
162
+ options?: AgentSessionSetOptions;
163
+ };
159
164
  }
160
165
  | {
161
166
  /** 请求 id,用于匹配响应。 */
@@ -24,4 +24,6 @@ export interface SessionCommandCompletion {
24
24
  title: string;
25
25
  /** Action Message 展示的可选结果说明。 */
26
26
  description?: string;
27
+ /** Action Message 持久化后是否发布对应 Session Mutation。 */
28
+ publish_mutation?: boolean;
27
29
  }
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import type { AgentSessionConfigSnapshot } from "@/types/agent/SessionTypes.js";
11
+ import type { SessionApprovalMode } from "@/types/session/SessionInteraction.js";
11
12
 
12
13
  /**
13
14
  * 本地 Session 内存状态。
@@ -27,6 +28,9 @@ export interface SessionLocalState {
27
28
  */
28
29
  effective_session_config: AgentSessionConfigSnapshot;
29
30
 
31
+ /** 当前 Session 已接受并持久化的 Shell 审批模式。 */
32
+ configured_approval_mode: SessionApprovalMode;
33
+
30
34
  /**
31
35
  * 当前 session 创建时间(毫秒时间戳)。
32
36
  */
@@ -80,6 +80,8 @@ export interface OpenSessionActionMessageInput {
80
80
  description?: string;
81
81
  /** 当前 Action 附加数据。 */
82
82
  data?: JsonObject;
83
+ /** Action 创建或更新后是否发布对应 Session Mutation。 */
84
+ publish_mutation?: boolean;
83
85
  }
84
86
 
85
87
  /** Error Message 创建参数。 */