Skip to content

Commit 2c85dea

Browse files
committed
Context tags
1 parent ab939eb commit 2c85dea

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
reportManualRunToolStop,
99
} from '@/lib/copilot/client-sse/run-tool-execution'
1010
import { MOTHERSHIP_CHAT_API_PATH } from '@/lib/copilot/constants'
11+
import { VFS_DIR_TO_RESOURCE } from '@/lib/copilot/resource-types'
1112
import { isWorkflowToolName } from '@/lib/copilot/workflow-tools'
1213
import { getNextWorkflowColor } from '@/lib/workflows/colors'
1314
import { invalidateResourceQueries } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry'
@@ -197,6 +198,36 @@ function ensureWorkflowInRegistry(resourceId: string, title: string, workspaceId
197198
return true
198199
}
199200

201+
function extractResourceFromReadResult(
202+
path: string | undefined,
203+
output: unknown
204+
): MothershipResource | null {
205+
if (!path) return null
206+
207+
const segments = path.split('/')
208+
const resourceType = VFS_DIR_TO_RESOURCE[segments[0]]
209+
if (!resourceType || !segments[1]) return null
210+
211+
const obj = output && typeof output === 'object' ? (output as Record<string, unknown>) : undefined
212+
if (!obj) return null
213+
214+
let id = obj.id as string | undefined
215+
let name = obj.name as string | undefined
216+
217+
if (!id && typeof obj.content === 'string') {
218+
try {
219+
const parsed = JSON.parse(obj.content)
220+
id = parsed?.id as string | undefined
221+
name = parsed?.name as string | undefined
222+
} catch {
223+
// content is not JSON
224+
}
225+
}
226+
227+
if (!id) return null
228+
return { type: resourceType, id, title: name || segments[1] }
229+
}
230+
200231
export interface UseChatOptions {
201232
onResourceEvent?: () => void
202233
}
@@ -352,14 +383,6 @@ export function useChat(
352383
return b
353384
}
354385

355-
const ensureSubagentTextBlock = (): ContentBlock => {
356-
const last = blocks[blocks.length - 1]
357-
if (last?.type === 'subagent_text') return last
358-
const b: ContentBlock = { type: 'subagent_text', content: '' }
359-
blocks.push(b)
360-
return b
361-
}
362-
363386
const flush = () => {
364387
streamingBlocksRef.current = [...blocks]
365388
setMessages((prev) =>
@@ -427,9 +450,10 @@ export function useChat(
427450
lastContentSource !== contentSource &&
428451
runningText.length > 0 &&
429452
!runningText.endsWith('\n')
430-
const tb = activeSubagent ? ensureSubagentTextBlock() : ensureTextBlock()
431-
tb.content = (tb.content ?? '') + chunk
432-
runningText += needsBoundaryNewline ? `\n${chunk}` : chunk
453+
const tb = ensureTextBlock()
454+
const normalizedChunk = needsBoundaryNewline ? `\n${chunk}` : chunk
455+
tb.content = (tb.content ?? '') + normalizedChunk
456+
runningText += normalizedChunk
433457
lastContentSource = contentSource
434458
streamingContentRef.current = runningText
435459
flush()
@@ -523,6 +547,18 @@ export function useChat(
523547
error: (parsed.error ?? getPayloadData(parsed)?.error) as string | undefined,
524548
}
525549
flush()
550+
551+
if (tc.name === 'read' && tc.status === 'success') {
552+
const readArgs = toolArgsMap.get(id)
553+
const resource = extractResourceFromReadResult(
554+
readArgs?.path as string | undefined,
555+
tc.result.output
556+
)
557+
if (resource) {
558+
addResource(resource)
559+
onResourceEventRef.current?.()
560+
}
561+
}
526562
}
527563

528564
break

apps/sim/lib/copilot/process-contents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,6 @@ export async function resolveActiveResourceContext(
755755
return null
756756
}
757757
}
758-
759758
async function resolveTableResource(tableId: string): Promise<AgentContext | null> {
760759
const table = await getTableById(tableId)
761760
if (!table) return null
@@ -787,3 +786,4 @@ async function resolveFileResource(
787786
content: JSON.stringify(meta, null, 2),
788787
}
789788
}
789+

0 commit comments

Comments
 (0)