d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
135 lines
4.5 KiB
TypeScript
135 lines
4.5 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { toError } from '@sim/utils/errors'
|
|
import {
|
|
MothershipStreamV1EventType,
|
|
MothershipStreamV1ResourceOp,
|
|
} from '@/lib/copilot/generated/mothership-stream-v1'
|
|
import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
|
|
import { TraceSpan } from '@/lib/copilot/generated/trace-spans-v1'
|
|
import { withCopilotSpan } from '@/lib/copilot/request/otel'
|
|
import type { StreamEvent, ToolCallResult } from '@/lib/copilot/request/types'
|
|
import {
|
|
extractDeletedResourcesFromToolResult,
|
|
extractResourcesFromToolResult,
|
|
hasDeleteCapability,
|
|
isResourceToolName,
|
|
persistChatResources,
|
|
removeChatResources,
|
|
} from '@/lib/copilot/resources/persistence'
|
|
|
|
const logger = createLogger('CopilotResourceEffects')
|
|
|
|
/**
|
|
* Persist and emit resource events after a successful tool execution.
|
|
*
|
|
* Handles both creation/upsert and deletion of chat resources depending on
|
|
* the tool's capabilities and output shape.
|
|
*/
|
|
export async function handleResourceSideEffects(
|
|
toolName: string,
|
|
params: Record<string, unknown> | undefined,
|
|
result: ToolCallResult,
|
|
chatId: string,
|
|
onEvent: ((event: StreamEvent) => void | Promise<void>) | undefined,
|
|
isAborted: () => boolean
|
|
): Promise<void> {
|
|
// Cheap early exit so we don't emit a span for tools that can never
|
|
// produce resources (most of them). The span only shows up for tools
|
|
// that might actually do resource work.
|
|
if (
|
|
!hasDeleteCapability(toolName) &&
|
|
!isResourceToolName(toolName) &&
|
|
!(result.resources && result.resources.length > 0)
|
|
) {
|
|
return
|
|
}
|
|
|
|
return withCopilotSpan(
|
|
TraceSpan.CopilotToolsHandleResourceSideEffects,
|
|
{
|
|
[TraceAttr.ToolName]: toolName,
|
|
[TraceAttr.ChatId]: chatId,
|
|
},
|
|
async (span) => {
|
|
let isDeleteOp = false
|
|
let removedCount = 0
|
|
let upsertedCount = 0
|
|
|
|
if (hasDeleteCapability(toolName)) {
|
|
const deleted = extractDeletedResourcesFromToolResult(toolName, params, result.output)
|
|
if (deleted.length > 0) {
|
|
isDeleteOp = true
|
|
removedCount = deleted.length
|
|
// Detached from the span lifecycle — the span ends before the
|
|
// DB call completes. That is intentional; we want the span to
|
|
// reflect the synchronous decision + event emission, not the
|
|
// best-effort persistence.
|
|
removeChatResources(chatId, deleted).catch((err) => {
|
|
logger.warn('Failed to remove chat resources after deletion', {
|
|
chatId,
|
|
error: toError(err).message,
|
|
})
|
|
})
|
|
|
|
for (const resource of deleted) {
|
|
if (isAborted()) break
|
|
await onEvent?.({
|
|
type: MothershipStreamV1EventType.resource,
|
|
payload: {
|
|
op: MothershipStreamV1ResourceOp.remove,
|
|
resource: { type: resource.type, id: resource.id, title: resource.title },
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isDeleteOp && !isAborted()) {
|
|
const resources =
|
|
result.resources && result.resources.length > 0
|
|
? result.resources
|
|
: isResourceToolName(toolName)
|
|
? extractResourcesFromToolResult(toolName, params, result.output)
|
|
: []
|
|
|
|
if (resources.length > 0) {
|
|
upsertedCount = resources.length
|
|
logger.info('[file-stream-server] Emitting resource upsert events', {
|
|
toolName,
|
|
chatId,
|
|
resources: resources.map((r) => ({ type: r.type, id: r.id, title: r.title })),
|
|
})
|
|
persistChatResources(chatId, resources).catch((err) => {
|
|
logger.warn('Failed to persist chat resources', {
|
|
chatId,
|
|
error: toError(err).message,
|
|
})
|
|
})
|
|
|
|
for (const resource of resources) {
|
|
if (isAborted()) break
|
|
await onEvent?.({
|
|
type: MothershipStreamV1EventType.resource,
|
|
payload: {
|
|
op: MothershipStreamV1ResourceOp.upsert,
|
|
resource: { type: resource.type, id: resource.id, title: resource.title },
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
span.setAttributes({
|
|
[TraceAttr.CopilotResourcesOp]: isDeleteOp
|
|
? 'delete'
|
|
: upsertedCount > 0
|
|
? 'upsert'
|
|
: 'none',
|
|
[TraceAttr.CopilotResourcesRemovedCount]: removedCount,
|
|
[TraceAttr.CopilotResourcesUpsertedCount]: upsertedCount,
|
|
[TraceAttr.CopilotResourcesAborted]: isAborted(),
|
|
})
|
|
}
|
|
)
|
|
}
|