mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
feat(dify-builder): add trace export assembler and safe serializer
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
eca20fcf69
commit
c25f991cde
2 files changed
+100
No files matched your search
+51
@@ -0,0 +1,51 @@
|
||||
import { buildTraceExport, serializeTraceExport } from '../trace-export'
|
||||
import type { TraceSnapshot } from '../trace-buffer'
|
||||
import type { SessionView } from '../../types'
|
||||
|
||||
const snapshot: TraceSnapshot = {
|
||||
entries: [
|
||||
{ seq: 1, ts: '2026-09-04T00:00:00.000Z', dir: 'out', kind: 'action', payload: { action_id: 'approve_plan' } },
|
||||
{ seq: 2, ts: '2026-09-04T00:00:01.000Z', dir: 'in', kind: 'state', payload: { state: 'build.plan_approval' } },
|
||||
],
|
||||
truncated: true,
|
||||
}
|
||||
|
||||
const view = {
|
||||
session_id: 's1', app_id: 'app-1', version: 7, state: 'build.plan_approval',
|
||||
entry_mode: 'build', run_status: 'waiting_input', canvas_read_only: false,
|
||||
interrupted: false, conversation_last_seq: 2, model: { provider: 'anthropic', name: 'claude' },
|
||||
} as unknown as SessionView
|
||||
|
||||
describe('buildTraceExport', () => {
|
||||
it('assembles meta from the view and counts entries', () => {
|
||||
const result = buildTraceExport(snapshot, view)
|
||||
expect(result.meta).toMatchObject({
|
||||
session_id: 's1', app_id: 'app-1', entry_mode: 'build', state: 'build.plan_approval',
|
||||
version: 7, entry_count: 2, truncated: true,
|
||||
})
|
||||
expect(result.meta.captured_at).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/)
|
||||
expect(result.trace).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('produces safe defaults when there is no view', () => {
|
||||
const result = buildTraceExport({ entries: [], truncated: false }, null)
|
||||
expect(result.meta).toMatchObject({ session_id: '', app_id: '', state: '', version: 0, entry_count: 0, truncated: false, model: null })
|
||||
})
|
||||
})
|
||||
|
||||
describe('serializeTraceExport', () => {
|
||||
it('serializes to pretty JSON', () => {
|
||||
const json = serializeTraceExport(buildTraceExport(snapshot, view))
|
||||
expect(json).toContain('"entry_count": 2')
|
||||
expect(json).toContain('"kind": "action"')
|
||||
expect(json).toContain('\n ') // pretty-printed
|
||||
})
|
||||
|
||||
it('does not throw on a circular payload', () => {
|
||||
const circular: Record<string, unknown> = {}
|
||||
circular.self = circular
|
||||
const snap: TraceSnapshot = { entries: [{ seq: 1, ts: 't', dir: 'in', kind: 'node', payload: circular }], truncated: false }
|
||||
const json = serializeTraceExport(buildTraceExport(snap, view))
|
||||
expect(json).toContain('[Circular]')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,49 @@
|
||||
import type { SessionView } from '../types'
|
||||
import type { TraceEntry, TraceSnapshot } from './trace-buffer'
|
||||
|
||||
export type TraceExport = {
|
||||
meta: {
|
||||
session_id: string
|
||||
app_id: string
|
||||
entry_mode: string
|
||||
state: string
|
||||
version: number
|
||||
model: SessionView['model']
|
||||
captured_at: string
|
||||
entry_count: number
|
||||
truncated: boolean
|
||||
}
|
||||
trace: TraceEntry[]
|
||||
}
|
||||
|
||||
export const buildTraceExport = (snapshot: TraceSnapshot, view: SessionView | null): TraceExport => ({
|
||||
meta: {
|
||||
session_id: view?.session_id ?? '',
|
||||
app_id: view?.app_id ?? '',
|
||||
entry_mode: view?.entry_mode ?? '',
|
||||
state: view?.state ?? '',
|
||||
version: view?.version ?? 0,
|
||||
model: view?.model ?? null,
|
||||
captured_at: new Date().toISOString(),
|
||||
entry_count: snapshot.entries.length,
|
||||
truncated: snapshot.truncated,
|
||||
},
|
||||
trace: snapshot.entries,
|
||||
})
|
||||
|
||||
export const serializeTraceExport = (data: TraceExport): string => {
|
||||
const seen = new WeakSet<object>()
|
||||
return JSON.stringify(
|
||||
data,
|
||||
(_key, value) => {
|
||||
if (typeof value === 'bigint') return value.toString()
|
||||
if (typeof value === 'function') return `[Function ${(value as { name?: string }).name || 'anonymous'}]`
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
if (seen.has(value)) return '[Circular]'
|
||||
seen.add(value)
|
||||
}
|
||||
return value
|
||||
},
|
||||
2,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user