@weiliang0121/capix-protocol 0.1.0 → 0.3.0

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.
Files changed (81) hide show
  1. package/README.md +14 -12
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.js +2 -0
  4. package/dist/index.d.ts +2 -5
  5. package/dist/index.js +1 -1
  6. package/dist/transcript.d.ts +43 -0
  7. package/package.json +9 -25
  8. package/dist/content/changes.d.ts +0 -35
  9. package/dist/content/changes.js +0 -1
  10. package/dist/content/index.d.ts +0 -4
  11. package/dist/content/index.js +0 -2
  12. package/dist/content/memory.d.ts +0 -13
  13. package/dist/content/memory.js +0 -1
  14. package/dist/content/message.d.ts +0 -4
  15. package/dist/content/message.js +0 -1
  16. package/dist/content/transcript.d.ts +0 -32
  17. package/dist/conversation/activity.d.ts +0 -36
  18. package/dist/conversation/activity.js +0 -1
  19. package/dist/conversation/conversation.d.ts +0 -11
  20. package/dist/conversation/conversation.js +0 -1
  21. package/dist/conversation/delivery.d.ts +0 -99
  22. package/dist/conversation/delivery.js +0 -1
  23. package/dist/conversation/files.d.ts +0 -30
  24. package/dist/conversation/files.js +0 -1
  25. package/dist/conversation/generation.d.ts +0 -4
  26. package/dist/conversation/generation.js +0 -1
  27. package/dist/conversation/index.d.ts +0 -10
  28. package/dist/conversation/index.js +0 -2
  29. package/dist/conversation/preview.d.ts +0 -46
  30. package/dist/conversation/preview.js +0 -1
  31. package/dist/conversation/recovery.d.ts +0 -29
  32. package/dist/conversation/recovery.js +0 -1
  33. package/dist/conversation/service.d.ts +0 -75
  34. package/dist/conversation/service.js +0 -1
  35. package/dist/conversation/settings.d.ts +0 -6
  36. package/dist/conversation/settings.js +0 -1
  37. package/dist/conversation/task.d.ts +0 -11
  38. package/dist/conversation/task.js +0 -1
  39. package/dist/invocation/changes.d.ts +0 -28
  40. package/dist/invocation/changes.js +0 -1
  41. package/dist/invocation/errors.d.ts +0 -1
  42. package/dist/invocation/errors.js +0 -1
  43. package/dist/invocation/index.d.ts +0 -3
  44. package/dist/invocation/index.js +0 -2
  45. package/dist/invocation/types.d.ts +0 -16
  46. package/dist/invocation/types.js +0 -2
  47. package/dist/observer/archive.d.ts +0 -9
  48. package/dist/observer/archive.js +0 -2
  49. package/dist/observer/changes.d.ts +0 -4
  50. package/dist/observer/changes.js +0 -1
  51. package/dist/observer/execution.d.ts +0 -10
  52. package/dist/observer/execution.js +0 -1
  53. package/dist/observer/index.d.ts +0 -8
  54. package/dist/observer/index.js +0 -2
  55. package/dist/observer/interaction.d.ts +0 -31
  56. package/dist/observer/interaction.js +0 -1
  57. package/dist/observer/memory.d.ts +0 -13
  58. package/dist/observer/memory.js +0 -1
  59. package/dist/observer/overview.d.ts +0 -45
  60. package/dist/observer/overview.js +0 -1
  61. package/dist/observer/selection.d.ts +0 -19
  62. package/dist/observer/selection.js +0 -1
  63. package/dist/observer/stream.d.ts +0 -56
  64. package/dist/observer/stream.js +0 -1
  65. package/dist/recording/changes.d.ts +0 -43
  66. package/dist/recording/changes.js +0 -1
  67. package/dist/recording/index.d.ts +0 -7
  68. package/dist/recording/index.js +0 -2
  69. package/dist/recording/invocation.d.ts +0 -10
  70. package/dist/recording/invocation.js +0 -2
  71. package/dist/recording/memory.d.ts +0 -18
  72. package/dist/recording/memory.js +0 -1
  73. package/dist/recording/resource.d.ts +0 -10
  74. package/dist/recording/resource.js +0 -1
  75. package/dist/recording/transcript.d.ts +0 -19
  76. package/dist/recording/transcript.js +0 -1
  77. package/dist/recording/types.d.ts +0 -190
  78. package/dist/recording/types.js +0 -2
  79. package/dist/recording/validate.d.ts +0 -4
  80. package/dist/recording/validate.js +0 -2
  81. /package/dist/{content/transcript.js → transcript.js} +0 -0
@@ -1,190 +0,0 @@
1
- import type { RecordingArchiveOrigin } from '../observer/archive.js';
2
- import type { InvocationFailureFact, RuntimeErrorValue } from '../invocation/errors.js';
3
- import type { CallableKind } from '../invocation/types.js';
4
- import type { TranscriptRecord } from './transcript.js';
5
- import type { MemoryRecord } from './memory.js';
6
- import type { ResourceRecord } from './resource.js';
7
- import type { InvocationRelationIdentity } from '../invocation/types.js';
8
- export { invocationProcessStages, invocationProcessActions } from '../invocation/types.js';
9
- export interface BaseRecord<Value = unknown> {
10
- readonly id: string;
11
- readonly parentId?: string;
12
- readonly value: Value;
13
- }
14
- export interface CompositeRecord<Value = unknown> extends BaseRecord<Value> {
15
- readonly childIds: readonly string[];
16
- }
17
- export interface RecordingRootValue {
18
- readonly createdAt: string;
19
- }
20
- export interface RecordingRootRecord extends CompositeRecord<RecordingRootValue> {
21
- }
22
- export type ProcessRecord = Readonly<{
23
- sequence: number;
24
- eventSequence: number;
25
- round: number;
26
- occurredAt: string;
27
- offsetMs: number;
28
- } & ({
29
- stage: 'execute';
30
- type: 'start';
31
- input: unknown;
32
- } | {
33
- stage: 'execute' | 'receive' | 'handling';
34
- type: 'return';
35
- output: unknown;
36
- } | {
37
- stage: 'receive';
38
- type: 'return';
39
- resultOf: number;
40
- } | {
41
- stage: 'execute' | 'receive' | 'handling';
42
- type: 'throw';
43
- error: RuntimeErrorValue;
44
- failure?: InvocationFailureFact;
45
- } | {
46
- stage: 'receive' | 'handling';
47
- type: 'continue';
48
- input: unknown;
49
- reason: string;
50
- } | {
51
- stage: 'handling';
52
- type: 'start' | 'unhandled';
53
- error: RuntimeErrorValue;
54
- failure?: InvocationFailureFact;
55
- } | {
56
- stage: 'control';
57
- type: 'stop';
58
- error: RuntimeErrorValue;
59
- failure?: InvocationFailureFact;
60
- })>;
61
- export type RecordedInvocationResult = Readonly<{
62
- status: 'succeeded';
63
- output: unknown;
64
- } | {
65
- status: 'failed';
66
- error: RuntimeErrorValue;
67
- failure?: InvocationFailureFact;
68
- }>;
69
- export type RecordedRelationChange = Readonly<{
70
- kind: 'attached';
71
- } | {
72
- kind: 'returned';
73
- result: RecordedInvocationResult;
74
- } | {
75
- kind: 'receiving';
76
- } | {
77
- kind: 'accepted';
78
- output: unknown;
79
- } | {
80
- kind: 'handled';
81
- output: unknown;
82
- } | {
83
- kind: 'failed';
84
- source: 'work' | 'observation';
85
- error: RuntimeErrorValue;
86
- failure?: InvocationFailureFact;
87
- }>;
88
- export interface RelationChangeRecord {
89
- readonly identity: InvocationRelationIdentity;
90
- readonly eventSequence: number;
91
- readonly occurredAt: string;
92
- readonly offsetMs: number;
93
- readonly childId?: string;
94
- readonly change: RecordedRelationChange;
95
- }
96
- export interface InvocationValue<Input = unknown, Output = unknown> {
97
- readonly processes: readonly ProcessRecord[];
98
- readonly relationChanges: readonly RelationChangeRecord[];
99
- input: Input;
100
- output?: Output;
101
- resultOf?: number;
102
- error?: RuntimeErrorValue;
103
- failure?: InvocationFailureFact;
104
- }
105
- export interface RuntimeCallValue<Input = unknown, Output = unknown> extends InvocationValue<Input, Output> {
106
- readonly startedAt: string;
107
- }
108
- export interface RuntimeCallRecord<Input = unknown, Output = unknown> extends CompositeRecord<RuntimeCallValue<Input, Output>> {
109
- readonly contextId: string;
110
- readonly runtimeInstanceId: string;
111
- readonly status: 'running' | 'succeeded' | 'failed';
112
- }
113
- export interface ExecutionValue<Input = unknown, Output = unknown> extends InvocationValue<Input, Output> {
114
- startedAt: string;
115
- }
116
- export interface ExecutionRecord<Input = unknown, Output = unknown> extends CompositeRecord<ExecutionValue<Input, Output>> {
117
- readonly contextId: string;
118
- readonly status: 'running' | 'succeeded' | 'failed';
119
- }
120
- export interface OperationRecord<Input = unknown, Output = unknown> extends CompositeRecord<InvocationValue<Input, Output>> {
121
- readonly contextId: string;
122
- readonly status: 'running' | 'succeeded' | 'failed';
123
- readonly executionId: string;
124
- readonly operationInstanceId: string;
125
- readonly parentOperationId?: string;
126
- readonly operation: string;
127
- readonly label: string;
128
- readonly source: string;
129
- readonly caller: string;
130
- readonly callee: string;
131
- }
132
- export interface AttemptValue<Input = unknown, Output = unknown> extends InvocationValue<Input, Output> {
133
- }
134
- export interface AttemptRecord<Input = unknown, Output = unknown> extends CompositeRecord<AttemptValue<Input, Output>> {
135
- readonly contextId: string;
136
- readonly status: 'running' | 'succeeded' | 'failed';
137
- readonly operationId: string;
138
- readonly name: string;
139
- readonly label: string;
140
- readonly source: string;
141
- readonly caller: string;
142
- readonly callee: string;
143
- }
144
- export interface InvocationBoundaryValue {
145
- readonly type: 'call' | 'return' | 'throw';
146
- readonly scope: 'work';
147
- }
148
- export interface InvocationBoundaryRecord extends BaseRecord<InvocationBoundaryValue> {
149
- readonly scopeId: string;
150
- readonly executionId?: string;
151
- readonly callId: string;
152
- readonly sequence: number;
153
- readonly occurredAt: string;
154
- readonly offsetMs: number;
155
- }
156
- export interface LegacyExecutionRecordSnapshot {
157
- readonly archive?: RecordingArchiveOrigin;
158
- runtimeCall: RuntimeCallRecord;
159
- runtimeEvents: InvocationBoundaryRecord[];
160
- execution: ExecutionRecord;
161
- operations: OperationRecord[];
162
- attempts: AttemptRecord[];
163
- events: InvocationBoundaryRecord[];
164
- resources: ResourceRecord[];
165
- }
166
- export interface CallableValue<Input = unknown, Output = unknown> extends InvocationValue<Input, Output> {
167
- readonly startedAt?: string;
168
- }
169
- export interface CallableRecord<Input = unknown, Output = unknown> extends CompositeRecord<CallableValue<Input, Output>> {
170
- readonly contextId: string;
171
- readonly structureKind: CallableKind;
172
- readonly structureId?: string;
173
- readonly name?: string;
174
- readonly label?: string;
175
- readonly source?: string;
176
- readonly callee?: string;
177
- readonly role?: string;
178
- readonly status: 'running' | 'succeeded' | 'failed';
179
- }
180
- export interface ExecutionRecordSnapshot {
181
- readonly archive?: RecordingArchiveOrigin;
182
- readonly execution: CallableRecord;
183
- readonly calls: readonly CallableRecord[];
184
- readonly rootCall?: CallableRecord;
185
- readonly ancestors?: readonly CallableRecord[];
186
- readonly boundaries: readonly InvocationBoundaryRecord[];
187
- readonly transcripts: readonly TranscriptRecord[];
188
- readonly memories: readonly MemoryRecord[];
189
- readonly resources: readonly ResourceRecord[];
190
- }
@@ -1,2 +0,0 @@
1
- /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
2
- import{invocationProcessStages,invocationProcessActions}from"../invocation/types.js";export{invocationProcessActions,invocationProcessStages};
@@ -1,4 +0,0 @@
1
- import type { ExecutionRecordSnapshot, LegacyExecutionRecordSnapshot, InvocationValue } from './types.js';
2
- export declare function validateExecutionRecordSnapshot(value: unknown): asserts value is ExecutionRecordSnapshot;
3
- export declare function validateLegacyExecutionRecordSnapshot(value: unknown): asserts value is LegacyExecutionRecordSnapshot;
4
- export declare function validateInvocationRecordValue(value: unknown, ownerContextId: string): asserts value is InvocationValue;
@@ -1,2 +0,0 @@
1
- /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
2
- var __defProp=Object.defineProperty;var __name=(target,value)=>__defProp(target,"name",{value,configurable:!0});import{validateRecordingArchiveOrigin}from"../observer/archive.js";import{hasOutput,outputProcess,readOutput}from"./invocation.js";function validateExecutionRecordSnapshot(value){function fail(){throw new Error("\u8C03\u7528\u8BB0\u5F55\u683C\u5F0F\u65E0\u6548\u6216\u5173\u8054\u7F3A\u5931\u3002")}__name(fail,"fail"),(!value||typeof value!="object")&&fail();const snapshot=value;snapshot.archive!==void 0&&validateRecordingArchiveOrigin(snapshot.archive),(!snapshot.execution||!Array.isArray(snapshot.calls)||!Array.isArray(snapshot.boundaries)||!Array.isArray(snapshot.resources)||!Array.isArray(snapshot.transcripts)||!Array.isArray(snapshot.memories))&&fail();const records=new Map,contexts=new Set,accept=__name(record=>{(!record||typeof record.id!="string"||!record.id||typeof record.contextId!="string"||!record.contextId||record.contextId===record.id||records.has(record.id)||contexts.has(record.id)||records.has(record.contextId)||contexts.has(record.contextId)||!["scene","group","node"].includes(record.structureKind)||!Array.isArray(record.childIds)||new Set(record.childIds).size!==record.childIds.length||record.childIds.some(id=>typeof id!="string"||!id)||!record.value)&&fail(),(record.value.startedAt===void 0?!snapshot.archive:typeof record.value.startedAt!="string"||!Number.isFinite(Date.parse(record.value.startedAt)))&&fail();for(const key of["structureId","name","label","callee","role"])record[key]!==void 0&&(typeof record[key]!="string"||!record[key].trim())&&fail();record.source!==void 0&&typeof record.source!="string"&&fail(),record.parentId!==void 0&&(typeof record.parentId!="string"||!record.parentId)&&fail(),validateInvocationRecordValue(record.value,record.contextId),validRecordedStatus(record.status,record.value)||fail(),records.set(record.id,record),contexts.add(record.contextId)},"accept");for(const record of snapshot.calls)accept(record);const task=records.get(snapshot.execution.id);(!task||JSON.stringify(task)!==JSON.stringify(snapshot.execution))&&fail(),snapshot.rootCall&&snapshot.rootCall.id!==task.id?accept(snapshot.rootCall):snapshot.rootCall&&JSON.stringify(snapshot.rootCall)!==JSON.stringify(task)&&fail(),snapshot.ancestors!==void 0&&!Array.isArray(snapshot.ancestors)&&fail();const ancestors=snapshot.ancestors??[];ancestors.length&&(!snapshot.rootCall||snapshot.rootCall.id===task.id)&&fail();for(const ancestor of ancestors)accept(ancestor);const path=snapshot.rootCall&&snapshot.rootCall.id!==task.id?[snapshot.rootCall,...ancestors,task]:[task];for(let index=1;index<path.length;index++){const parent=path[index-1],child=path[index];(parent.structureKind==="node"||child.parentId!==parent.id||!parent.childIds.includes(child.id))&&fail()}const root=path[0];root.parentId&&records.has(root.parentId)&&fail();for(const record of snapshot.calls){if(contexts.has(record.id)&&fail(),record.id===task.id)continue;const visited=new Set([record.id]);let current=record;for(;current.id!==task.id;){const parent=current.parentId?records.get(current.parentId):void 0;(!parent||visited.has(parent.id)||parent.structureKind==="node"||!parent.childIds.includes(current.id))&&fail(),visited.add(parent.id),current=parent}}for(const record of records.values()){record.structureKind==="scene"&&record.parentId&&records.has(record.parentId)&&fail();for(const relation of record.value.relationChanges){const child=relation.childId?records.get(relation.childId):void 0;child&&(child.parentId!==record.id||child.contextId!==relation.identity.childContextId)&&fail()}}const seen=new Set([...records.keys(),...contexts]),timing=new Map,scopes=new Map;for(const boundary of snapshot.boundaries){const call=records.get(boundary?.callId),prior=scopes.get(boundary?.scopeId);(!boundary||typeof boundary.id!="string"||!boundary.id||seen.has(boundary.id)||!call||boundary.parentId!==call.id||typeof boundary.scopeId!="string"||!boundary.scopeId||!Number.isSafeInteger(boundary.sequence)||boundary.sequence<1||prior&&boundary.sequence<=prior.sequence||!Number.isFinite(boundary.offsetMs)||boundary.offsetMs<(prior?.offsetMs??0)||typeof boundary.occurredAt!="string"||!Number.isFinite(Date.parse(boundary.occurredAt))||boundary.value?.scope!=="work")&&fail(),!snapshot.archive&&(boundary.scopeId!==(snapshot.rootCall??task).id||boundary.executionId!==void 0)&&fail(),seen.add(boundary.id),scopes.set(boundary.scopeId,{sequence:boundary.sequence,offsetMs:boundary.offsetMs});const state=timing.get(call.id)??{scopeId:boundary.scopeId};state.scopeId!==boundary.scopeId&&fail();const time=boundary.offsetMs;boundary.value.type==="call"?((state.started!==void 0||state.ended!==void 0)&&fail(),state.started=time):boundary.value.type==="return"||boundary.value.type==="throw"?((state.started===void 0||state.ended!==void 0||time<state.started||call.status!==(boundary.value.type==="return"?"succeeded":"failed"))&&fail(),state.ended=time):fail(),timing.set(call.id,state)}for(const record of records.values()){const time=timing.get(record.id);!snapshot.archive&&(!time||time.started===void 0||record.status!=="running"&&time.ended===void 0)&&fail();const parentTime=record.parentId?timing.get(record.parentId):void 0;time?.scopeId===parentTime?.scopeId&&(time?.started!==void 0&&parentTime?.started!==void 0&&time.started<parentTime.started&&fail(),time?.ended!==void 0&&parentTime?.ended!==void 0&&time.ended>parentTime.ended&&fail(),time?.started!==void 0&&parentTime?.ended!==void 0&&time.ended===void 0&&fail())}const resourceSequences=new Map,accesses=new Map,interactions=new Map;for(const resource of snapshot.resources){(!resource||typeof resource.id!="string"||!resource.id||seen.has(resource.id)||!records.has(resource.parentId)||typeof resource.access!="string"||!resource.access||typeof resource.supplyId!="string"||!resource.supplyId||typeof resource.occurredAt!="string"||!Number.isFinite(Date.parse(resource.occurredAt))||!Object.hasOwn(resource,"value")||resource.sequence!==(resourceSequences.get(resource.parentId)??0)+1)&&fail(),resourceSequences.set(resource.parentId,resource.sequence),seen.add(resource.id);const key=JSON.stringify([resource.parentId,resource.supplyId,resource.access]);resource.phase==="bound"?((accesses.has(key)||resource.interactionId!==void 0)&&fail(),accesses.set(key,"bound")):resource.phase==="available"||resource.phase==="unavailable"?((accesses.get(key)!=="bound"||resource.interactionId!==void 0)&&fail(),accesses.set(key,resource.phase)):resource.phase==="started"?((accesses.get(key)!=="available"||typeof resource.interactionId!="string"||!resource.interactionId||interactions.has(resource.interactionId))&&fail(),interactions.set(resource.interactionId,key)):resource.phase==="returned"||resource.phase==="threw"?((typeof resource.interactionId!="string"||interactions.get(resource.interactionId)!==key)&&fail(),interactions.delete(resource.interactionId)):fail()}for(const content of[...snapshot.transcripts,...snapshot.memories]){const call=content.callId?records.get(content.callId):void 0;(!content||typeof content.id!="string"||!content.id||seen.has(content.id)||!call||content.parentId!==call.id||content.contextId!==call.contextId||!Object.hasOwn(content,"value"))&&fail(),seen.add(content.id)}}__name(validateExecutionRecordSnapshot,"validateExecutionRecordSnapshot");function validateLegacyExecutionRecordSnapshot(value){const fail=__name(()=>{throw new Error("\u8FD0\u884C\u8BB0\u5F55\u683C\u5F0F\u65E0\u6548\u6216\u5173\u8054\u7F3A\u5931\u3002")},"fail");if(!value||typeof value!="object")return fail();const snapshot=value;snapshot.archive!==void 0&&validateRecordingArchiveOrigin(snapshot.archive);const historical=snapshot.archive!==void 0;if(!snapshot.execution||typeof snapshot.execution.id!="string"||!snapshot.execution.id||!snapshot.execution.value||typeof snapshot.execution.value.startedAt!="string"||!Array.isArray(snapshot.operations)||!Array.isArray(snapshot.attempts)||!Array.isArray(snapshot.events)||!Array.isArray(snapshot.resources))return fail();const executionId=snapshot.execution.id,operations=new Map,parents=new Map,statuses=new Map([[executionId,snapshot.execution.status]]),bodies=new Map([[executionId,snapshot.execution.value]]),contextsByRecord=new Map,contextIds=new Set,acceptContextIdentity=__name(record=>{if(typeof record.contextId!="string"||!record.contextId||record.contextId===record.id||contextIds.has(record.contextId))return fail();contextIds.add(record.contextId),contextsByRecord.set(record.id,record.contextId)},"acceptContextIdentity");acceptContextIdentity(snapshot.execution);const runtimeCall=snapshot.runtimeCall;if(!runtimeCall||typeof runtimeCall.id!="string"||!runtimeCall.id||runtimeCall.id===executionId||typeof runtimeCall.runtimeInstanceId!="string"||!runtimeCall.runtimeInstanceId||!runtimeCall.value||typeof runtimeCall.value.startedAt!="string"||snapshot.execution.parentId!==runtimeCall.id||!["running","succeeded","failed"].includes(runtimeCall.status))return fail();acceptContextIdentity(runtimeCall);const validParticipant=__name(value2=>typeof value2=="string"&&value2.trim().length>0,"validParticipant");for(const operationRecord of snapshot.operations){if(!operationRecord||typeof operationRecord.id!="string"||!operationRecord.id||bodies.has(operationRecord.id)||operationRecord.executionId!==executionId||typeof operationRecord.operationInstanceId!="string"||!operationRecord.operationInstanceId||typeof operationRecord.operation!="string"||typeof operationRecord.label!="string"||typeof operationRecord.source!="string"||!validParticipant(operationRecord.caller)||!validParticipant(operationRecord.callee)||operationRecord.parentOperationId!==void 0&&typeof operationRecord.parentOperationId!="string")return fail();acceptContextIdentity(operationRecord),operations.set(operationRecord.id,operationRecord),parents.set(operationRecord.id,operationRecord.parentOperationId??executionId),statuses.set(operationRecord.id,operationRecord.status),bodies.set(operationRecord.id,operationRecord.value)}for(const attemptRecord of snapshot.attempts){if(!attemptRecord||typeof attemptRecord.id!="string"||!attemptRecord.id||bodies.has(attemptRecord.id)||typeof attemptRecord.operationId!="string"||!operations.has(attemptRecord.operationId)||typeof attemptRecord.name!="string"||!attemptRecord.name.trim()||typeof attemptRecord.label!="string"||!attemptRecord.label.trim()||typeof attemptRecord.source!="string"||!validParticipant(attemptRecord.caller)||!validParticipant(attemptRecord.callee))return fail();acceptContextIdentity(attemptRecord),parents.set(attemptRecord.id,attemptRecord.operationId),statuses.set(attemptRecord.id,attemptRecord.status),bodies.set(attemptRecord.id,attemptRecord.value)}if(bodies.has(runtimeCall.id))return fail();const observedBodies=new Map(bodies);observedBodies.set(runtimeCall.id,runtimeCall.value);for(const id of contextIds)if(observedBodies.has(id))return fail();for(const status of statuses.values())if(!["running","succeeded","failed"].includes(status))return fail();for(const[owner,body]of observedBodies)validateInvocationRecordValue(body,contextsByRecord.get(owner));for(const operationRecord of operations.values())if(operationRecord.parentOperationId!==void 0&&!operations.has(operationRecord.parentOperationId))return fail();for(const id of parents.keys()){const visited=new Set([id]);let ancestor=parents.get(id);for(;ancestor&&ancestor!==executionId;){if(visited.has(ancestor))return fail();visited.add(ancestor),ancestor=parents.get(ancestor)}}const active=new Set,entered=new Set,ended=new Set,boundaryIds=new Set;let time=0;for(const[i,boundary]of snapshot.events.entries()){if(!boundary||typeof boundary.id!="string"||!boundary.id||bodies.has(boundary.id)||contextIds.has(boundary.id)||boundaryIds.has(boundary.id)||boundary.executionId!==executionId||boundary.scopeId!==executionId||!boundary.value||boundary.value.scope!=="work"||!["call","return","throw"].includes(boundary.value.type)||typeof boundary.occurredAt!="string"||!Number.isFinite(Date.parse(boundary.occurredAt))||!bodies.has(boundary.callId)||boundary.sequence!==i+1||!Number.isFinite(boundary.offsetMs)||boundary.offsetMs<time)return fail();boundaryIds.add(boundary.id),time=boundary.offsetMs;const parentId=parents.get(boundary.callId);if(boundary.value.type==="call"){if(entered.has(boundary.callId)||parentId!==void 0&&(!active.has(executionId)||!active.has(parentId))||boundary.callId===executionId&&i!==0)return fail();entered.add(boundary.callId),active.add(boundary.callId)}else{const body=bodies.get(boundary.callId);if(!["return","throw"].includes(boundary.value.type)||!active.has(boundary.callId)||[...active].some(id=>id!==boundary.callId&&(boundary.callId===executionId||parents.get(id)===boundary.callId))||!(boundary.value.type==="return"?hasOutput(body):Object.hasOwn(body,"error"))||statuses.get(boundary.callId)!==(boundary.value.type==="return"?"succeeded":"failed"))return fail();active.delete(boundary.callId),ended.add(boundary.callId)}}if(!historical&&entered.size!==bodies.size)return fail();for(const[id,body]of bodies)if(!historical&&!ended.has(id)&&(statuses.get(id)!=="running"||hasOutput(body)||Object.hasOwn(body,"error"))||historical&&!validRecordedStatus(statuses.get(id),body))return fail();if(!Array.isArray(snapshot.runtimeEvents)||!validRuntimeCallBoundaries(snapshot.runtimeEvents,runtimeCall.id,runtimeCall.status,runtimeCall.value,historical))return fail();for(const boundary of snapshot.runtimeEvents){if(boundaryIds.has(boundary.id)||observedBodies.has(boundary.id)||contextIds.has(boundary.id))return fail();boundaryIds.add(boundary.id)}const resourceIds=new Set,resourceSequences=new Map,accesses=new Map,interactions=new Map;for(const resourceRecord of snapshot.resources){if(!resourceRecord||typeof resourceRecord.id!="string"||!resourceRecord.id||bodies.has(resourceRecord.id)||contextIds.has(resourceRecord.id)||boundaryIds.has(resourceRecord.id)||resourceIds.has(resourceRecord.id)||typeof resourceRecord.parentId!="string"||!observedBodies.has(resourceRecord.parentId)||typeof resourceRecord.access!="string"||!resourceRecord.access||typeof resourceRecord.supplyId!="string"||!resourceRecord.supplyId||typeof resourceRecord.occurredAt!="string"||!Number.isFinite(Date.parse(resourceRecord.occurredAt))||!Object.hasOwn(resourceRecord,"value")||resourceRecord.sequence!==(resourceSequences.get(resourceRecord.parentId)??0)+1)return fail();resourceSequences.set(resourceRecord.parentId,resourceRecord.sequence),resourceIds.add(resourceRecord.id);const accessKey=JSON.stringify([resourceRecord.parentId,resourceRecord.supplyId,resourceRecord.access]);if(resourceRecord.phase==="bound"){if(accesses.has(accessKey)||resourceRecord.interactionId!==void 0)return fail();accesses.set(accessKey,"bound")}else if(resourceRecord.phase==="available"||resourceRecord.phase==="unavailable"){if(accesses.get(accessKey)!=="bound"||resourceRecord.interactionId!==void 0)return fail();accesses.set(accessKey,resourceRecord.phase)}else if(resourceRecord.phase==="started"){if(accesses.get(accessKey)!=="available"||typeof resourceRecord.interactionId!="string"||!resourceRecord.interactionId||interactions.has(resourceRecord.interactionId))return fail();interactions.set(resourceRecord.interactionId,accessKey)}else if(resourceRecord.phase==="returned"||resourceRecord.phase==="threw"){if(typeof resourceRecord.interactionId!="string"||interactions.get(resourceRecord.interactionId)!==accessKey)return fail();interactions.delete(resourceRecord.interactionId)}else return fail()}}__name(validateLegacyExecutionRecordSnapshot,"validateLegacyExecutionRecordSnapshot");function validFailure(value){if(!value||typeof value!="object")return!1;const failure=value;return(failure.kind==="exception"||failure.kind==="error")&&typeof failure.contextId=="string"&&failure.contextId.length>0&&["setup","execute","receive","handling","control","observation"].includes(failure.phase)&&(failure.contract===void 0||typeof failure.contract=="string")}__name(validFailure,"validFailure");function validRelations(body,ownerContextId){const relations=new Map,eventSequences=new Set(body.processes.map(process=>process.eventSequence));let previous=0;for(const entry of body.relationChanges){if(!entry||!Number.isSafeInteger(entry.eventSequence)||entry.eventSequence<=previous||eventSequences.has(entry.eventSequence)||!Number.isFinite(entry.offsetMs)||entry.offsetMs<0||typeof entry.occurredAt!="string"||!Number.isFinite(Date.parse(entry.occurredAt))||!entry.identity||!entry.change)return!1;const identity=entry.identity;if(identity.parentContextId!==ownerContextId||typeof identity.childContextId!="string"||!identity.childContextId||identity.childContextId===ownerContextId||!Number.isSafeInteger(identity.parentRound)||identity.parentRound<0||identity.parentRound>2||identity.key!==void 0&&typeof identity.key!="string"||entry.childId!==void 0&&(typeof entry.childId!="string"||!entry.childId))return!1;const old=relations.get(identity.childContextId);if(old&&(old.identity.parentRound!==identity.parentRound||old.identity.key!==identity.key))return!1;const change=entry.change;if(change.kind==="attached"){if(old)return!1;relations.set(identity.childContextId,{identity,phase:"attached"})}else if(change.kind==="returned"){if(old?.phase!=="attached"||!change.result||!["succeeded","failed"].includes(change.result.status)||(change.result.status==="succeeded"?!Object.hasOwn(change.result,"output"):!validError(change.result.error)||change.result.failure!==void 0&&!validFailure(change.result.failure)))return!1;relations.set(identity.childContextId,{identity,phase:"returned",status:change.result.status})}else if(change.kind==="receiving"){if(old?.phase!=="returned")return!1;relations.set(identity.childContextId,{...old,phase:"receiving"})}else if(change.kind==="accepted"){if(old?.phase!=="receiving"||old.status!=="succeeded"||!Object.hasOwn(change,"output"))return!1;relations.set(identity.childContextId,{...old,phase:"accepted"})}else if(change.kind==="handled"){if(old?.phase!=="receiving"||old.status!=="failed"||!Object.hasOwn(change,"output"))return!1;relations.set(identity.childContextId,{...old,phase:"handled"})}else if(change.kind==="failed"){if(old?.phase!=="receiving"||!validError(change.error)||!["work","observation"].includes(change.source)||change.failure!==void 0&&!validFailure(change.failure))return!1;relations.set(identity.childContextId,{...old,phase:"failed"})}else return!1;eventSequences.add(entry.eventSequence),previous=entry.eventSequence}const times=[...body.processes,...body.relationChanges].sort((a,b)=>a.eventSequence-b.eventSequence);return times.every((entry,index)=>index===0||entry.offsetMs>=times[index-1].offsetMs)}__name(validRelations,"validRelations");function validError(value){return!!value&&typeof value=="object"&&typeof value.code=="string"&&typeof value.message=="string"}__name(validError,"validError");function validRuntimeCallBoundaries(boundaries,id,status,value,historical=!1){if(!historical&&boundaries.length<1||boundaries.length>2)return!1;for(const[index,boundary]of boundaries.entries())if(!boundary||!boundary.id||boundary.scopeId!==id||boundary.executionId!==void 0||boundary.callId!==id||boundary.parentId!==id||boundary.sequence!==index+1||!Number.isFinite(boundary.offsetMs)||boundary.offsetMs<(boundaries[index-1]?.offsetMs??0)||!Number.isFinite(Date.parse(boundary.occurredAt))||boundary.value?.scope!=="work"||boundary.value.type!==(index===0?"call":status==="succeeded"?"return":"throw"))return!1;return historical&&boundaries.length<2?validRecordedStatus(status,value):status==="running"?boundaries.length===1:boundaries.length===2&&(status==="succeeded"?hasOutput(value):Object.hasOwn(value,"error"))}__name(validRuntimeCallBoundaries,"validRuntimeCallBoundaries");function validateInvocationRecordValue(value,ownerContextId){const fail=__name(()=>{throw new Error("\u8C03\u7528\u8BB0\u5F55\u6B63\u6587\u65E0\u6548\u3002")},"fail"),body=value;if(!body||!Array.isArray(body.processes)||!Array.isArray(body.relationChanges)||typeof body!="object"||!Object.hasOwn(body,"input")||hasOutput(body)&&Object.hasOwn(body,"error")||Object.hasOwn(body,"output")&&Object.hasOwn(body,"resultOf")||Object.hasOwn(body,"error")&&(!body.error||typeof body.error.code!="string"||typeof body.error.message!="string")||body.failure&&(!Object.hasOwn(body,"error")||!validFailure(body.failure)))return fail();let previousRound=0,previousTime=0,previousSequence=0;for(const[index,process]of body.processes.entries()){if(!process||!Number.isSafeInteger(process.eventSequence)||process.eventSequence<=previousSequence||process.sequence!==index+1||!Number.isInteger(process.round)||process.round<previousRound||process.round>2||!Number.isFinite(process.offsetMs)||process.offsetMs<previousTime||typeof process.occurredAt!="string"||!(process.stage==="execute"?["start","return","throw"]:process.stage==="receive"?["return","throw","continue"]:process.stage==="handling"?["start","return","throw","continue","unhandled"]:process.stage==="control"?["stop"]:[]).includes(process.type)||process.round===0&&process.stage!=="control"||(process.stage==="execute"&&process.type==="start"||process.type==="continue")&&!Object.hasOwn(process,"input")||process.type==="return"&&Object.hasOwn(process,"output")===Object.hasOwn(process,"resultOf"))return fail();if("resultOf"in process)try{const target=outputProcess(body,process.resultOf,process.sequence);if(process.stage!=="receive"||target.stage!=="execute"||target.round!==process.round||target.sequence!==process.sequence-1)return fail()}catch{return fail()}if((process.type==="throw"||process.type==="stop"||process.stage==="handling"&&(process.type==="start"||process.type==="unhandled"))&&(!process.error||typeof process.error.code!="string"||typeof process.error.message!="string")||process.type==="continue"&&(typeof process.reason!="string"||!process.reason.trim())||"failure"in process&&process.failure&&!validFailure(process.failure))return fail();previousRound=process.round,previousTime=process.offsetMs,previousSequence=process.eventSequence}if(!validRelations(body,ownerContextId))return fail();if(Object.hasOwn(body,"resultOf"))try{readOutput(body)}catch{return fail()}}__name(validateInvocationRecordValue,"validateInvocationRecordValue");function validRecordedStatus(status,body){return status==="running"?!hasOutput(body)&&!Object.hasOwn(body,"error"):status==="succeeded"?hasOutput(body)&&!Object.hasOwn(body,"error"):status==="failed"&&Object.hasOwn(body,"error")&&!hasOutput(body)}__name(validRecordedStatus,"validRecordedStatus");export{validateExecutionRecordSnapshot,validateInvocationRecordValue,validateLegacyExecutionRecordSnapshot};
File without changes