@lota-sdk/core 0.4.26 → 0.4.27

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lota-sdk/core",
3
- "version": "0.4.26",
3
+ "version": "0.4.27",
4
4
  "files": [
5
5
  "src",
6
6
  "infrastructure/schema"
@@ -32,7 +32,7 @@
32
32
  "@ai-sdk/provider": "^3.0.9",
33
33
  "@chat-adapter/slack": "^4.26.0",
34
34
  "@chat-adapter/state-ioredis": "^4.26.0",
35
- "@lota-sdk/shared": "0.4.26",
35
+ "@lota-sdk/shared": "0.4.27",
36
36
  "@mendable/firecrawl-js": "^4.20.0",
37
37
  "@surrealdb/node": "^3.0.3",
38
38
  "ai": "^6.0.170",
@@ -3,6 +3,7 @@ import type { ChatMessage } from '@lota-sdk/shared'
3
3
 
4
4
  import type { ResolvedAgentConfig } from '../config/agent-defaults'
5
5
  import { resolveAgentNameAlias } from '../config/agent-defaults'
6
+ import { truncateText } from '../utils/string'
6
7
  import type { ChatMessageLike, ReadableUploadMetadataLike } from './chat-types'
7
8
 
8
9
  export interface ThreadHistoryMessage {
@@ -11,6 +12,9 @@ export interface ThreadHistoryMessage {
11
12
  agentName?: string
12
13
  }
13
14
 
15
+ const THREAD_HISTORY_MESSAGE_MAX_CHARS = 4_000
16
+ const CONVERSATION_SUMMARY_MAX_CHARS = 12_000
17
+
14
18
  export function asRecord(value: unknown): Record<string, unknown> | null {
15
19
  return value && typeof value === 'object' ? (value as Record<string, unknown>) : null
16
20
  }
@@ -53,7 +57,7 @@ export function toHistoryMessages(
53
57
  ): ThreadHistoryMessage[] {
54
58
  return messages
55
59
  .map((message): ThreadHistoryMessage | null => {
56
- const content = extractMessageText(message)
60
+ const content = truncateText(extractMessageText(message), THREAD_HISTORY_MESSAGE_MAX_CHARS)
57
61
  if (!content) return null
58
62
 
59
63
  if (message.role === 'user') {
@@ -75,15 +79,28 @@ export function buildConversationSummary(params: {
75
79
  assistantMessages: ChatMessageLike[]
76
80
  }): string {
77
81
  const lines: string[] = []
82
+ let summaryChars = 0
83
+
84
+ const appendLine = (line: string): boolean => {
85
+ const separatorChars = lines.length === 0 ? 0 : 2
86
+ const remaining = CONVERSATION_SUMMARY_MAX_CHARS - summaryChars - separatorChars
87
+ if (remaining <= 3) return false
88
+
89
+ const nextLine = line.length > remaining ? truncateText(line, remaining) : line
90
+ lines.push(nextLine)
91
+ summaryChars += separatorChars + nextLine.length
92
+ return line.length <= remaining
93
+ }
94
+
78
95
  if (params.userMessageText.trim()) {
79
- lines.push(`User: ${params.userMessageText.trim()}`)
96
+ appendLine(`User: ${truncateText(params.userMessageText.trim(), THREAD_HISTORY_MESSAGE_MAX_CHARS)}`)
80
97
  }
81
98
 
82
99
  for (const message of params.assistantMessages) {
83
- const content = extractMessageText(message)
100
+ const content = truncateText(extractMessageText(message), THREAD_HISTORY_MESSAGE_MAX_CHARS)
84
101
  if (!content) continue
85
102
  const agentName = getAgentName(params.agentConfig, message)
86
- lines.push(agentName ? `${agentName}: ${content}` : `Assistant: ${content}`)
103
+ if (!appendLine(agentName ? `${agentName}: ${content}` : `Assistant: ${content}`)) break
87
104
  }
88
105
 
89
106
  return lines.join('\n\n').trim()
@@ -191,14 +191,19 @@ export function makeSocialChatHistoryService(
191
191
  params.cursor?.createdAt.getTime() ??
192
192
  (params.onboardingCutoff ? params.onboardingCutoff.getTime() : Number.NEGATIVE_INFINITY)
193
193
  const limit = typeof params.limit === 'number' ? Math.max(1, Math.trunc(params.limit)) : undefined
194
- const rangeLimitArgs = limit === undefined ? [] : (['LIMIT', 0, limit + 1] as const)
195
194
  const storageKeys = yield* Effect.tryPromise({
196
- try: () =>
197
- params.cursor || params.onboardingCutoff
198
- ? conn.zrangebyscore(indexKey, scoreStart, '+inf', ...rangeLimitArgs)
199
- : limit === undefined
200
- ? conn.zrange(indexKey, 0, -1)
201
- : conn.zrange(indexKey, 0, limit - 1),
195
+ try: () => {
196
+ if (params.cursor || params.onboardingCutoff) {
197
+ if (limit === undefined) {
198
+ return conn.zrangebyscore(indexKey, scoreStart, '+inf')
199
+ }
200
+ return conn
201
+ .call('ZRANGEBYSCORE', indexKey, String(scoreStart), '+inf', 'LIMIT', '0', String(limit + 1))
202
+ .then((result) => (Array.isArray(result) ? result.map(String) : []))
203
+ }
204
+
205
+ return limit === undefined ? conn.zrange(indexKey, 0, -1) : conn.zrange(indexKey, 0, limit - 1)
206
+ },
202
207
  catch: (cause) => new SocialChatHistoryError({ message: 'Failed to list workspace message keys.', cause }),
203
208
  })
204
209
  if (storageKeys.length === 0) return [] as SocialChatHistoryMessage[]