@weiliang0121/capix-protocol 0.0.0-stage → 0.1.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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +23 -2
  3. package/dist/content/changes.d.ts +35 -0
  4. package/dist/content/changes.js +1 -0
  5. package/dist/content/index.d.ts +4 -0
  6. package/dist/content/index.js +2 -0
  7. package/dist/content/memory.d.ts +13 -0
  8. package/dist/content/memory.js +1 -0
  9. package/dist/content/message.d.ts +4 -0
  10. package/dist/content/message.js +1 -0
  11. package/dist/content/transcript.d.ts +32 -0
  12. package/dist/content/transcript.js +1 -0
  13. package/dist/conversation/activity.d.ts +36 -0
  14. package/dist/conversation/activity.js +1 -0
  15. package/dist/conversation/conversation.d.ts +11 -0
  16. package/dist/conversation/conversation.js +1 -0
  17. package/dist/conversation/delivery.d.ts +99 -0
  18. package/dist/conversation/delivery.js +1 -0
  19. package/dist/conversation/files.d.ts +30 -0
  20. package/dist/conversation/files.js +1 -0
  21. package/dist/conversation/generation.d.ts +4 -0
  22. package/dist/conversation/generation.js +1 -0
  23. package/dist/conversation/index.d.ts +10 -0
  24. package/dist/conversation/index.js +2 -0
  25. package/dist/conversation/preview.d.ts +46 -0
  26. package/dist/conversation/preview.js +1 -0
  27. package/dist/conversation/recovery.d.ts +29 -0
  28. package/dist/conversation/recovery.js +1 -0
  29. package/dist/conversation/service.d.ts +75 -0
  30. package/dist/conversation/service.js +1 -0
  31. package/dist/conversation/settings.d.ts +6 -0
  32. package/dist/conversation/settings.js +1 -0
  33. package/dist/conversation/task.d.ts +11 -0
  34. package/dist/conversation/task.js +1 -0
  35. package/dist/index.d.ts +5 -0
  36. package/dist/index.js +2 -0
  37. package/dist/invocation/changes.d.ts +28 -0
  38. package/dist/invocation/changes.js +1 -0
  39. package/dist/invocation/errors.d.ts +1 -0
  40. package/dist/invocation/errors.js +1 -0
  41. package/dist/invocation/index.d.ts +3 -0
  42. package/dist/invocation/index.js +2 -0
  43. package/dist/invocation/types.d.ts +16 -0
  44. package/dist/invocation/types.js +2 -0
  45. package/dist/observer/archive.d.ts +9 -0
  46. package/dist/observer/archive.js +2 -0
  47. package/dist/observer/changes.d.ts +4 -0
  48. package/dist/observer/changes.js +1 -0
  49. package/dist/observer/execution.d.ts +10 -0
  50. package/dist/observer/execution.js +1 -0
  51. package/dist/observer/index.d.ts +8 -0
  52. package/dist/observer/index.js +2 -0
  53. package/dist/observer/interaction.d.ts +31 -0
  54. package/dist/observer/interaction.js +1 -0
  55. package/dist/observer/memory.d.ts +13 -0
  56. package/dist/observer/memory.js +1 -0
  57. package/dist/observer/overview.d.ts +45 -0
  58. package/dist/observer/overview.js +1 -0
  59. package/dist/observer/selection.d.ts +19 -0
  60. package/dist/observer/selection.js +1 -0
  61. package/dist/observer/stream.d.ts +56 -0
  62. package/dist/observer/stream.js +1 -0
  63. package/dist/recording/changes.d.ts +43 -0
  64. package/dist/recording/changes.js +1 -0
  65. package/dist/recording/index.d.ts +7 -0
  66. package/dist/recording/index.js +2 -0
  67. package/dist/recording/invocation.d.ts +10 -0
  68. package/dist/recording/invocation.js +2 -0
  69. package/dist/recording/memory.d.ts +18 -0
  70. package/dist/recording/memory.js +1 -0
  71. package/dist/recording/resource.d.ts +10 -0
  72. package/dist/recording/resource.js +1 -0
  73. package/dist/recording/transcript.d.ts +19 -0
  74. package/dist/recording/transcript.js +1 -0
  75. package/dist/recording/types.d.ts +190 -0
  76. package/dist/recording/types.js +2 -0
  77. package/dist/recording/validate.d.ts +4 -0
  78. package/dist/recording/validate.js +2 -0
  79. package/package.json +59 -3
@@ -0,0 +1,9 @@
1
+ export interface RecordingArchive {
2
+ readonly sourceId: string;
3
+ readonly rootIds: readonly string[];
4
+ readonly revision: number;
5
+ readonly state: 'open' | 'sealed' | 'incomplete';
6
+ }
7
+ export type RecordingArchiveOrigin = Omit<RecordingArchive, 'rootIds'>;
8
+ export declare function validateRecordingArchiveOrigin(value: unknown): asserts value is RecordingArchiveOrigin;
9
+ export declare function validateRecordingArchives(value: unknown): asserts value is readonly RecordingArchive[];
@@ -0,0 +1,2 @@
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});function validateRecordingArchiveOrigin(value){if(!value||typeof value!="object")throw new Error("\u5386\u53F2\u8BB0\u5F55\u6765\u6E90\u683C\u5F0F\u65E0\u6548\u3002");const source=value;if(typeof source.sourceId!="string"||!source.sourceId||!Number.isSafeInteger(source.revision)||source.revision<0||!["open","sealed","incomplete"].includes(source.state))throw new Error("\u5386\u53F2\u8BB0\u5F55\u6765\u6E90\u683C\u5F0F\u65E0\u6548\u3002")}__name(validateRecordingArchiveOrigin,"validateRecordingArchiveOrigin");function validateRecordingArchives(value){if(!Array.isArray(value))throw new Error("\u5386\u53F2\u8BB0\u5F55\u6765\u6E90\u683C\u5F0F\u65E0\u6548\u3002");const sources=new Set,roots=new Set;for(const archive of value){validateRecordingArchiveOrigin(archive);const source=archive;if(sources.has(source.sourceId)||!Array.isArray(source.rootIds)||source.rootIds.some(id=>typeof id!="string"||!id||roots.has(id)))throw new Error("\u5386\u53F2\u8BB0\u5F55\u6765\u6E90\u5F52\u5C5E\u65E0\u6548\u3002");sources.add(source.sourceId);for(const id of source.rootIds){if(roots.has(id))throw new Error("\u5386\u53F2\u8BB0\u5F55\u6765\u6E90\u5F52\u5C5E\u65E0\u6548\u3002");roots.add(id)}}}__name(validateRecordingArchives,"validateRecordingArchives");export{validateRecordingArchiveOrigin,validateRecordingArchives};
@@ -0,0 +1,4 @@
1
+ import type { TranscriptEvent, MemoryEvent } from '../content/changes.js';
2
+ import type { RecordChangeEvent } from '../recording/changes.js';
3
+ import type { InvocationEvent } from '../invocation/changes.js';
4
+ export type ObserverChange = TranscriptEvent | MemoryEvent | RecordChangeEvent | InvocationEvent;
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,10 @@
1
+ import type { SelectionObservation } from './selection.js';
2
+ import type { OperationRecord } from '../recording/types.js';
3
+ export interface ExecutionObservation extends Omit<SelectionObservation, 'executions'> {
4
+ executions: (SelectionObservation['executions'][number] & {
5
+ response?: {
6
+ index?: number;
7
+ };
8
+ memoryStatus?: OperationRecord['status'];
9
+ })[];
10
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,8 @@
1
+ export * from './overview.js';
2
+ export * from './execution.js';
3
+ export * from './memory.js';
4
+ export * from './selection.js';
5
+ export * from './changes.js';
6
+ export * from './stream.js';
7
+ export * from './archive.js';
8
+ export * from './interaction.js';
@@ -0,0 +1,2 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
2
+ export*from"./overview.js";export*from"./execution.js";export*from"./memory.js";export*from"./selection.js";export*from"./changes.js";export*from"./stream.js";export*from"./archive.js";export*from"./interaction.js";
@@ -0,0 +1,31 @@
1
+ import type { RuntimeErrorValue } from '../invocation/errors.js';
2
+ export interface CallInteractionLinks {
3
+ readonly callId: string;
4
+ readonly interactionIds: readonly string[];
5
+ }
6
+ export interface InteractionObservation {
7
+ readonly id: string;
8
+ readonly contract: string;
9
+ readonly revision: number;
10
+ readonly delivery: 'prepared' | 'started';
11
+ readonly retained: 'persistent' | 'memory';
12
+ readonly model: string;
13
+ readonly purpose: string;
14
+ readonly result: {
15
+ readonly kind: 'pending';
16
+ } | {
17
+ readonly kind: 'received';
18
+ readonly feedbackId: string;
19
+ } | {
20
+ readonly kind: 'unavailable';
21
+ readonly error: RuntimeErrorValue;
22
+ };
23
+ }
24
+ export type InteractionUpdate = {
25
+ readonly type: 'snapshot';
26
+ readonly value: InteractionObservation;
27
+ } | {
28
+ readonly type: 'error';
29
+ readonly error: string;
30
+ readonly code?: string;
31
+ };
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,13 @@
1
+ import type { MessageLocator } from '../content/transcript.js';
2
+ import type { MemoryRecord } from '../recording/memory.js';
3
+ import type { MemoryResultSource } from '../content/memory.js';
4
+ export interface MemoryObservation {
5
+ readonly items: readonly {
6
+ id: string;
7
+ topic: string;
8
+ content: string;
9
+ sources: readonly MessageLocator[];
10
+ sourceResults?: readonly MemoryResultSource[];
11
+ records: readonly MemoryRecord[];
12
+ }[];
13
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,45 @@
1
+ import type { RecordingArchive, RecordingArchiveOrigin } from './archive.js';
2
+ import type { CallableKind } from '../invocation/types.js';
3
+ import type { ExecutionRecord } from '../recording/types.js';
4
+ import type { ExecutionObservation } from './execution.js';
5
+ import type { MemoryRecord } from '../recording/memory.js';
6
+ import type { TranscriptRangeLocator } from '../content/transcript.js';
7
+ export interface SourceResponse {
8
+ path: string;
9
+ content: string;
10
+ }
11
+ export type { ObservationUpdate } from './stream.js';
12
+ export interface InvocationSummary {
13
+ id: string;
14
+ contextId: string;
15
+ recordType: CallableKind;
16
+ parentId?: string;
17
+ name: string;
18
+ label: string;
19
+ source: string;
20
+ status: ExecutionRecord['status'];
21
+ offsetMs: number;
22
+ durationMs?: number;
23
+ }
24
+ type ObservedExecution = ExecutionObservation['executions'][number];
25
+ export interface ExecutionSummary extends ObservedExecution {
26
+ readonly archive?: RecordingArchiveOrigin;
27
+ durationMs?: number;
28
+ observedOffsetMs: number;
29
+ calls: InvocationSummary[];
30
+ transcriptRange?: TranscriptRangeLocator;
31
+ availableMemoryIds: string[];
32
+ memoryChanges: {
33
+ id: string;
34
+ memoryId: string;
35
+ operationId?: string;
36
+ action: MemoryRecord['value']['action'];
37
+ change: string;
38
+ }[];
39
+ }
40
+ export interface ObserverOverview {
41
+ readonly archives?: readonly RecordingArchive[];
42
+ transcriptId: string;
43
+ messages: ExecutionObservation['messages'];
44
+ executions: ExecutionSummary[];
45
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,19 @@
1
+ import type { ExecutionRecord, OperationRecord } from '../recording/types.js';
2
+ export interface SelectionObservation {
3
+ transcriptId: string;
4
+ messages: {
5
+ index: number;
6
+ count: number;
7
+ }[];
8
+ executions: {
9
+ id: string;
10
+ startedAt: string;
11
+ status: ExecutionRecord['status'];
12
+ inputIndex: number;
13
+ selections: {
14
+ operationId: string;
15
+ status: OperationRecord['status'];
16
+ indexes?: readonly number[];
17
+ }[];
18
+ }[];
19
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,56 @@
1
+ import type { ObserverOverview } from './overview.js';
2
+ import type { MemoryObservation } from './memory.js';
3
+ import type { RecordEntry } from '../recording/changes.js';
4
+ import type { ObserverChange } from './changes.js';
5
+ import type { InvocationEvent } from '../invocation/changes.js';
6
+ import type { TranscriptEvent } from '../content/changes.js';
7
+ export type TranscriptNotice = Omit<TranscriptEvent, 'change'> & {
8
+ readonly change: Readonly<{
9
+ kind: 'messages-appended';
10
+ start: number;
11
+ end: number;
12
+ }> | Extract<TranscriptEvent['change'], {
13
+ kind: 'segment-started';
14
+ }>;
15
+ };
16
+ export type ObservationChange = Exclude<ObserverChange, TranscriptEvent> | TranscriptNotice;
17
+ export interface ObservationBaseline {
18
+ readonly type: 'baseline';
19
+ readonly scopeId: string;
20
+ readonly sequence: number;
21
+ readonly overview: ObserverOverview;
22
+ readonly memory: MemoryObservation;
23
+ readonly transcript: {
24
+ readonly id: string;
25
+ readonly revision: number;
26
+ readonly starts: readonly number[];
27
+ };
28
+ readonly memories: {
29
+ readonly id: string;
30
+ readonly sequence: number;
31
+ };
32
+ readonly recorder: {
33
+ readonly id: string;
34
+ readonly revision: number;
35
+ };
36
+ readonly entries: readonly RecordEntry[];
37
+ readonly invocations: readonly InvocationEvent[];
38
+ }
39
+ export interface ObservationDelta {
40
+ readonly type: 'delta';
41
+ readonly scopeId: string;
42
+ readonly sequence: number;
43
+ readonly changes: readonly ObservationChange[];
44
+ }
45
+ export type ObservationFrame = ObservationBaseline | ObservationDelta;
46
+ export interface ObservationSourceIncomplete {
47
+ readonly type: 'error';
48
+ readonly code: 'SOURCE_INCOMPLETE';
49
+ readonly sourceId: string;
50
+ readonly error: string;
51
+ }
52
+ export type ObservationUpdate = ObservationFrame | ObservationSourceIncomplete | {
53
+ type: 'error';
54
+ error: string;
55
+ code?: string;
56
+ };
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,43 @@
1
+ import type { BaseRecord, ProcessRecord, RelationChangeRecord } from './types.js';
2
+ import type { RuntimeErrorValue, InvocationFailureFact } from '../invocation/errors.js';
3
+ export type RecordKind = 'invocation' | 'runtime' | 'runtime-call' | 'execution' | 'operation' | 'attempt' | 'invocation-boundary' | 'transcript' | 'memory' | 'resource' | 'custom';
4
+ export interface RecordEntry {
5
+ readonly kind: RecordKind;
6
+ readonly data: BaseRecord;
7
+ }
8
+ export type RecordMutation = Readonly<{
9
+ kind: 'added';
10
+ entry: RecordEntry;
11
+ parent?: Readonly<{
12
+ id: string;
13
+ childId: string;
14
+ }>;
15
+ }> | Readonly<{
16
+ kind: 'process-appended';
17
+ id: string;
18
+ process: ProcessRecord;
19
+ }> | Readonly<{
20
+ kind: 'relation-appended';
21
+ id: string;
22
+ relation: RelationChangeRecord;
23
+ }> | Readonly<{
24
+ kind: 'finished';
25
+ id: string;
26
+ status: 'succeeded' | 'failed';
27
+ result: Readonly<{
28
+ output?: unknown;
29
+ resultOf?: number;
30
+ error?: RuntimeErrorValue;
31
+ failure?: InvocationFailureFact;
32
+ }>;
33
+ }> | Readonly<{
34
+ kind: 'replaced';
35
+ entry: RecordEntry;
36
+ }>;
37
+ export type RecordChangeEvent = Readonly<{
38
+ type: 'record';
39
+ recorderId: string;
40
+ revision: number;
41
+ occurredAt: string;
42
+ change: RecordMutation;
43
+ }>;
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,7 @@
1
+ export * from './types.js';
2
+ export * from './invocation.js';
3
+ export * from './memory.js';
4
+ export * from './transcript.js';
5
+ export * from './resource.js';
6
+ export * from './changes.js';
7
+ export * from './validate.js';
@@ -0,0 +1,2 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
2
+ export*from"./types.js";export*from"./invocation.js";export*from"./memory.js";export*from"./transcript.js";export*from"./resource.js";export*from"./changes.js";export*from"./validate.js";
@@ -0,0 +1,10 @@
1
+ import type { InvocationValue, ProcessRecord } from './types.js';
2
+ type OutputProcess = Extract<ProcessRecord, {
3
+ type: 'return';
4
+ output: unknown;
5
+ }>;
6
+ export declare function outputProcess(value: Pick<InvocationValue, 'processes'>, sequence: number, before?: number): OutputProcess;
7
+ export declare function hasOutput(value: InvocationValue): boolean;
8
+ export declare function finalOutputProcess(value: Pick<InvocationValue, 'processes'>, sequence: number): OutputProcess;
9
+ export declare function readOutput<Input, Output>(value: InvocationValue<Input, Output>): Output | undefined;
10
+ export {};
@@ -0,0 +1,2 @@
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});function outputProcess(value,sequence,before=value.processes.length+1){const process=value.processes[sequence-1];if(!Number.isSafeInteger(sequence)||sequence<1||sequence>=before||!process||process.sequence!==sequence||process.type!=="return"||!Object.hasOwn(process,"output")||Object.hasOwn(process,"resultOf"))throw new Error("Invalid local result reference");return process}__name(outputProcess,"outputProcess");function hasOutput(value){return Object.hasOwn(value,"output")||Object.hasOwn(value,"resultOf")}__name(hasOutput,"hasOutput");function finalOutputProcess(value,sequence){const last=value.processes.at(-1);if(!last||last.type!=="return"||sequence!==("resultOf"in last?last.resultOf:last.sequence))throw new Error("Final result must reference the last returned output");return outputProcess(value,sequence)}__name(finalOutputProcess,"finalOutputProcess");function readOutput(value){if(Object.hasOwn(value,"resultOf")){if(Object.hasOwn(value,"output"))throw new Error("Output and local result reference are mutually exclusive");return finalOutputProcess(value,value.resultOf).output}return value.output}__name(readOutput,"readOutput");export{finalOutputProcess,hasOutput,outputProcess,readOutput};
@@ -0,0 +1,18 @@
1
+ import type { MemoryContent, MemoryResultSource } from '../content/memory.js';
2
+ import type { BaseRecord } from './types.js';
3
+ export interface MemoryRecordValue {
4
+ readonly action: 'create' | 'update';
5
+ readonly change: string;
6
+ readonly sourceIndexes: readonly number[];
7
+ readonly sourceResults?: readonly MemoryResultSource[];
8
+ readonly result: MemoryContent;
9
+ }
10
+ export interface MemoryRecord extends BaseRecord<MemoryRecordValue> {
11
+ readonly memoryId: string;
12
+ readonly memoriesId: string;
13
+ readonly sequence: number;
14
+ readonly contextId?: string;
15
+ readonly callId?: string;
16
+ readonly executionId?: string;
17
+ readonly operationId?: string;
18
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,10 @@
1
+ import type { BaseRecord } from './types.js';
2
+ export interface ResourceRecord<Detail = unknown> extends BaseRecord<Detail> {
3
+ readonly parentId: string;
4
+ readonly access: string;
5
+ readonly supplyId: string;
6
+ readonly sequence: number;
7
+ readonly occurredAt: string;
8
+ readonly phase: 'bound' | 'available' | 'unavailable' | 'started' | 'returned' | 'threw';
9
+ readonly interactionId?: string;
10
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,19 @@
1
+ import type { BaseRecord } from './types.js';
2
+ export type TranscriptChange = {
3
+ readonly action: 'append';
4
+ readonly start: number;
5
+ readonly end: number;
6
+ } | {
7
+ readonly action: 'segment';
8
+ readonly previousStart: number;
9
+ readonly start: number;
10
+ readonly end: number;
11
+ };
12
+ export interface TranscriptRecord extends BaseRecord<TranscriptChange> {
13
+ readonly transcriptId: string;
14
+ readonly revision: number;
15
+ readonly occurredAt: string;
16
+ readonly contextId?: string;
17
+ readonly callId?: string;
18
+ readonly executionId?: string;
19
+ }
@@ -0,0 +1 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
@@ -0,0 +1,190 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ /*! MIT License | Copyright (c) 2026 wei.liang | See LICENSE */
2
+ import{invocationProcessStages,invocationProcessActions}from"../invocation/types.js";export{invocationProcessActions,invocationProcessStages};
@@ -0,0 +1,4 @@
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;
@@ -0,0 +1,2 @@
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};