6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
164 lines
5.6 KiB
TypeScript
164 lines
5.6 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,
|
|
projectedResult: 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)
|
|
const projectedDeleted = extractDeletedResourcesFromToolResult(
|
|
toolName,
|
|
params,
|
|
projectedResult.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 (let index = 0; index < deleted.length; index += 1) {
|
|
if (isAborted()) break
|
|
const resource = deleted[index]
|
|
const projected = projectedDeleted[index]
|
|
await onEvent?.({
|
|
type: MothershipStreamV1EventType.resource,
|
|
payload: {
|
|
op: MothershipStreamV1ResourceOp.remove,
|
|
resource: {
|
|
type: resource.type,
|
|
id: resource.id,
|
|
title: projected?.title ?? '',
|
|
},
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isDeleteOp && !isAborted()) {
|
|
const rawResources =
|
|
result.resources && result.resources.length > 0
|
|
? result.resources
|
|
: isResourceToolName(toolName)
|
|
? extractResourcesFromToolResult(toolName, params, result.output)
|
|
: []
|
|
const projectedResources =
|
|
result.resources && result.resources.length > 0
|
|
? (projectedResult.resources ?? [])
|
|
: isResourceToolName(toolName)
|
|
? extractResourcesFromToolResult(toolName, params, projectedResult.output)
|
|
: []
|
|
const resources =
|
|
projectedResources.length === rawResources.length
|
|
? rawResources.map((resource, index) => ({
|
|
type: resource.type,
|
|
id: resource.id,
|
|
title: projectedResources[index].title,
|
|
...(projectedResources[index].path !== undefined
|
|
? { path: projectedResources[index].path }
|
|
: {}),
|
|
}))
|
|
: []
|
|
|
|
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(),
|
|
})
|
|
}
|
|
)
|
|
}
|