@soimy/dingtalk 3.6.5 → 3.6.6

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.
@@ -4,36 +4,67 @@ interface SessionState {
4
4
  taskStartTime: number;
5
5
  }
6
6
 
7
+ interface SessionStateScope {
8
+ accountId: string;
9
+ conversationId: string;
10
+ agentId: string;
11
+ }
12
+
13
+ type SessionStateInitialMetadata = Partial<Pick<SessionState, "model" | "effort">>;
14
+
7
15
  const sessionStore = new Map<string, SessionState>();
8
16
 
9
- function sessionKey(accountId: string, conversationId: string): string {
10
- return `${accountId}:${conversationId}`;
17
+ function sessionKey(scope: SessionStateScope): string {
18
+ return `${scope.accountId}:${scope.agentId}:${scope.conversationId}`;
19
+ }
20
+
21
+ function normalizeMetadataValue(value: string | undefined): string | undefined {
22
+ const trimmed = typeof value === "string" ? value.trim() : "";
23
+ return trimmed || undefined;
24
+ }
25
+
26
+ function seedMissingSessionStateMetadata(
27
+ state: SessionState,
28
+ metadata?: SessionStateInitialMetadata,
29
+ ): void {
30
+ const model = normalizeMetadataValue(metadata?.model);
31
+ const effort = normalizeMetadataValue(metadata?.effort);
32
+ if (state.model === undefined && model !== undefined) {
33
+ state.model = model;
34
+ }
35
+ if (state.effort === undefined && effort !== undefined) {
36
+ state.effort = effort;
37
+ }
11
38
  }
12
39
 
13
- export function initSessionState(accountId: string, conversationId: string): SessionState {
14
- const key = sessionKey(accountId, conversationId);
40
+ export function initSessionState(
41
+ scope: SessionStateScope,
42
+ metadata?: SessionStateInitialMetadata,
43
+ ): SessionState {
44
+ const key = sessionKey(scope);
15
45
  const existing = sessionStore.get(key);
16
46
  if (existing) {
17
47
  existing.taskStartTime = Date.now();
48
+ seedMissingSessionStateMetadata(existing, metadata);
18
49
  return existing;
19
50
  }
20
51
  const state: SessionState = {
21
52
  taskStartTime: Date.now(),
22
53
  };
54
+ seedMissingSessionStateMetadata(state, metadata);
23
55
  sessionStore.set(key, state);
24
56
  return state;
25
57
  }
26
58
 
27
- export function getSessionState(accountId: string, conversationId: string): SessionState | undefined {
28
- return sessionStore.get(sessionKey(accountId, conversationId));
59
+ export function getSessionState(scope: SessionStateScope): SessionState | undefined {
60
+ return sessionStore.get(sessionKey(scope));
29
61
  }
30
62
 
31
63
  export function updateSessionState(
32
- accountId: string,
33
- conversationId: string,
64
+ scope: SessionStateScope,
34
65
  patch: Partial<Pick<SessionState, "model" | "effort">>,
35
66
  ): void {
36
- const state = sessionStore.get(sessionKey(accountId, conversationId));
67
+ const state = sessionStore.get(sessionKey(scope));
37
68
  if (!state) {
38
69
  return;
39
70
  }
@@ -45,16 +76,16 @@ export function updateSessionState(
45
76
  }
46
77
  }
47
78
 
48
- export function getTaskTimeSeconds(accountId: string, conversationId: string): number | undefined {
49
- const state = sessionStore.get(sessionKey(accountId, conversationId));
79
+ export function getTaskTimeSeconds(scope: SessionStateScope): number | undefined {
80
+ const state = sessionStore.get(sessionKey(scope));
50
81
  if (!state) {
51
82
  return undefined;
52
83
  }
53
84
  return Math.round((Date.now() - state.taskStartTime) / 1000);
54
85
  }
55
86
 
56
- export function clearSessionState(accountId: string, conversationId: string): void {
57
- sessionStore.delete(sessionKey(accountId, conversationId));
87
+ export function clearSessionState(scope: SessionStateScope): void {
88
+ sessionStore.delete(sessionKey(scope));
58
89
  }
59
90
 
60
91
  export function clearAllSessionStatesForTest(): void {