@superdoc-dev/sdk 1.19.2 → 1.20.1

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 (103) hide show
  1. package/dist/action-primitives/doc-index.cjs +215 -0
  2. package/dist/action-primitives/doc-index.d.ts +83 -0
  3. package/dist/action-primitives/doc-index.d.ts.map +1 -0
  4. package/dist/action-primitives/doc-index.js +211 -0
  5. package/dist/action-primitives/engine.cjs +204 -0
  6. package/dist/action-primitives/engine.d.ts +71 -0
  7. package/dist/action-primitives/engine.d.ts.map +1 -0
  8. package/dist/action-primitives/engine.js +196 -0
  9. package/dist/action-primitives/receipt.cjs +39 -0
  10. package/dist/action-primitives/receipt.d.ts +49 -0
  11. package/dist/action-primitives/receipt.d.ts.map +1 -0
  12. package/dist/action-primitives/receipt.js +32 -0
  13. package/dist/action-primitives/resolve.cjs +252 -0
  14. package/dist/action-primitives/resolve.d.ts +56 -0
  15. package/dist/action-primitives/resolve.d.ts.map +1 -0
  16. package/dist/action-primitives/resolve.js +246 -0
  17. package/dist/action-primitives/session-cache.cjs +43 -0
  18. package/dist/action-primitives/session-cache.d.ts +19 -0
  19. package/dist/action-primitives/session-cache.d.ts.map +1 -0
  20. package/dist/action-primitives/session-cache.js +37 -0
  21. package/dist/action-primitives/tools/list-transform.cjs +661 -0
  22. package/dist/action-primitives/tools/list-transform.d.ts +98 -0
  23. package/dist/action-primitives/tools/list-transform.d.ts.map +1 -0
  24. package/dist/action-primitives/tools/list-transform.js +656 -0
  25. package/dist/action-primitives/tools/structure-insert.cjs +1343 -0
  26. package/dist/action-primitives/tools/structure-insert.d.ts +183 -0
  27. package/dist/action-primitives/tools/structure-insert.d.ts.map +1 -0
  28. package/dist/action-primitives/tools/structure-insert.js +1338 -0
  29. package/dist/action-primitives/tools/text-transform.cjs +669 -0
  30. package/dist/action-primitives/tools/text-transform.d.ts +64 -0
  31. package/dist/action-primitives/tools/text-transform.d.ts.map +1 -0
  32. package/dist/action-primitives/tools/text-transform.js +664 -0
  33. package/dist/action-primitives/types.d.ts +36 -0
  34. package/dist/action-primitives/types.d.ts.map +1 -0
  35. package/dist/action-primitives/types.js +15 -0
  36. package/dist/agent/actions.cjs +5381 -0
  37. package/dist/agent/actions.d.ts +404 -0
  38. package/dist/agent/actions.d.ts.map +1 -0
  39. package/dist/agent/actions.js +5373 -0
  40. package/dist/agent/catalog.cjs +483 -0
  41. package/dist/agent/catalog.d.ts +103 -0
  42. package/dist/agent/catalog.d.ts.map +1 -0
  43. package/dist/agent/catalog.js +471 -0
  44. package/dist/agent/doc-snapshot.cjs +663 -0
  45. package/dist/agent/doc-snapshot.d.ts +247 -0
  46. package/dist/agent/doc-snapshot.d.ts.map +1 -0
  47. package/dist/agent/doc-snapshot.js +657 -0
  48. package/dist/agent/index.d.ts +16 -0
  49. package/dist/agent/index.d.ts.map +1 -0
  50. package/dist/agent/index.js +15 -0
  51. package/dist/agent/ir.cjs +170 -0
  52. package/dist/agent/ir.d.ts +216 -0
  53. package/dist/agent/ir.d.ts.map +1 -0
  54. package/dist/agent/ir.js +181 -0
  55. package/dist/agent/operation-catalog.cjs +418 -0
  56. package/dist/agent/operation-catalog.d.ts +36 -0
  57. package/dist/agent/operation-catalog.d.ts.map +1 -0
  58. package/dist/agent/operation-catalog.js +446 -0
  59. package/dist/agent/runtime.cjs +501 -0
  60. package/dist/agent/runtime.d.ts +120 -0
  61. package/dist/agent/runtime.d.ts.map +1 -0
  62. package/dist/agent/runtime.js +493 -0
  63. package/dist/embedded-prompts.generated.cjs +13 -0
  64. package/dist/embedded-prompts.generated.d.ts +4 -0
  65. package/dist/embedded-prompts.generated.d.ts.map +1 -0
  66. package/dist/embedded-prompts.generated.js +9 -0
  67. package/dist/generated/client.cjs +18 -0
  68. package/dist/generated/client.d.ts +134 -0
  69. package/dist/generated/client.d.ts.map +1 -1
  70. package/dist/generated/client.js +18 -0
  71. package/dist/generated/contract.cjs +622 -3
  72. package/dist/generated/contract.d.ts.map +1 -1
  73. package/dist/generated/contract.js +622 -3
  74. package/dist/index.cjs +3 -7
  75. package/dist/index.d.ts +8 -6
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +2 -8
  78. package/dist/presets/core.cjs +454 -0
  79. package/dist/presets/core.d.ts +20 -0
  80. package/dist/presets/core.d.ts.map +1 -0
  81. package/dist/presets/core.js +447 -0
  82. package/dist/presets.cjs +55 -8
  83. package/dist/presets.d.ts +39 -8
  84. package/dist/presets.d.ts.map +1 -1
  85. package/dist/presets.js +53 -8
  86. package/dist/prompts/mcp-prompt.md +23 -0
  87. package/dist/prompts/system-prompt.md +108 -0
  88. package/dist/runtime/transport-common.cjs +8 -0
  89. package/dist/runtime/transport-common.d.ts.map +1 -1
  90. package/dist/runtime/transport-common.js +8 -0
  91. package/dist/tools.cjs +46 -11
  92. package/dist/tools.d.ts +55 -8
  93. package/dist/tools.d.ts.map +1 -1
  94. package/dist/tools.js +45 -13
  95. package/package.json +9 -8
  96. package/tools/__pycache__/__init__.cpython-312.pyc +0 -0
  97. package/tools/__pycache__/intent_dispatch_generated.cpython-312.pyc +0 -0
  98. package/tools/catalog.json +14 -0
  99. package/tools/tools-policy.json +1 -1
  100. package/tools/tools.anthropic.json +14 -0
  101. package/tools/tools.generic.json +14 -0
  102. package/tools/tools.openai.json +14 -0
  103. package/tools/tools.vercel.json +14 -0
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+
3
+ var docIndex = require('./doc-index.cjs');
4
+ var receipt = require('./receipt.cjs');
5
+ var sessionCache = require('./session-cache.cjs');
6
+
7
+ function workflowStepSuccess(value) {
8
+ return { ok: true, value };
9
+ }
10
+ function workflowStepFailure(failure) {
11
+ return { ok: false, failure };
12
+ }
13
+ function workflowNotImplementedFailure(input) {
14
+ return {
15
+ status: 'not_implemented',
16
+ phase: input.phase,
17
+ code: input.code,
18
+ message: input.message,
19
+ details: input.details,
20
+ };
21
+ }
22
+ function toIndexSummary(index) {
23
+ return {
24
+ revision: index.revision,
25
+ blocks: index.blocks.length,
26
+ lists: index.lists.length,
27
+ tables: index.tables.length,
28
+ };
29
+ }
30
+ function coerceErrorMessage(error) {
31
+ if (error instanceof Error) {
32
+ return error.message;
33
+ }
34
+ return String(error);
35
+ }
36
+ async function ensureDocIndex(input) {
37
+ const sessionState = input.cache.getState(input.documentHandle);
38
+ const info = await input.documentHandle.info({}, input.invokeOptions);
39
+ const cached = input.cache.getCachedIndex(input.documentHandle, info.revision);
40
+ if (cached != null) {
41
+ return { sessionState, info, index: cached };
42
+ }
43
+ const index = await docIndex.buildWorkflowDocIndex({
44
+ documentHandle: input.documentHandle,
45
+ documentKey: sessionState.documentKey,
46
+ invokeOptions: input.invokeOptions,
47
+ info,
48
+ });
49
+ input.cache.setCachedIndex(input.documentHandle, index);
50
+ return { sessionState, info, index };
51
+ }
52
+ function receiptFromFailure(toolName, sessionState, index, failure) {
53
+ const details = { code: failure.code, ...failure.details };
54
+ if (failure.status === 'not_implemented') {
55
+ return receipt.createWorkflowNotImplementedReceipt({
56
+ toolName,
57
+ sessionKey: sessionState.documentKey,
58
+ phase: failure.phase,
59
+ message: failure.message,
60
+ index: toIndexSummary(index),
61
+ details,
62
+ });
63
+ }
64
+ return receipt.createWorkflowFailureReceipt({
65
+ toolName,
66
+ sessionKey: sessionState.documentKey,
67
+ phase: failure.phase,
68
+ message: failure.message,
69
+ index: toIndexSummary(index),
70
+ details,
71
+ });
72
+ }
73
+ function exceptionFailure(phase, error) {
74
+ return {
75
+ status: 'failed',
76
+ phase,
77
+ code: 'WORKFLOW_ENGINE_STEP_EXCEPTION',
78
+ message: `Workflow engine phase "${phase}" threw unexpectedly.`,
79
+ details: { error: coerceErrorMessage(error) },
80
+ };
81
+ }
82
+ async function runWorkflowEngine(input) {
83
+ const cache = input.cache ?? sessionCache.workflowPocSessionCache;
84
+ const { sessionState, info, index } = await ensureDocIndex({
85
+ documentHandle: input.documentHandle,
86
+ invokeOptions: input.invokeOptions,
87
+ cache,
88
+ });
89
+ const context = {
90
+ toolName: input.toolName,
91
+ args: input.args,
92
+ documentHandle: input.documentHandle,
93
+ invokeOptions: input.invokeOptions,
94
+ sessionState,
95
+ info,
96
+ index,
97
+ };
98
+ let resolvedStep;
99
+ try {
100
+ resolvedStep = await input.hooks.resolve(context);
101
+ }
102
+ catch (error) {
103
+ resolvedStep = workflowStepFailure(exceptionFailure('resolve', error));
104
+ }
105
+ if (!resolvedStep.ok) {
106
+ return {
107
+ receipt: receiptFromFailure(input.toolName, sessionState, index, resolvedStep.failure),
108
+ index,
109
+ sessionState,
110
+ };
111
+ }
112
+ let planStep;
113
+ try {
114
+ if (input.hooks.plan == null) {
115
+ planStep = workflowStepSuccess({ resolved: resolvedStep.value, args: context.args });
116
+ }
117
+ else {
118
+ planStep = await input.hooks.plan(context, resolvedStep.value);
119
+ }
120
+ }
121
+ catch (error) {
122
+ planStep = workflowStepFailure(exceptionFailure('plan', error));
123
+ }
124
+ if (!planStep.ok) {
125
+ return {
126
+ receipt: receiptFromFailure(input.toolName, sessionState, index, planStep.failure),
127
+ index,
128
+ sessionState,
129
+ };
130
+ }
131
+ if (input.hooks.execute == null) {
132
+ const notImplemented = workflowNotImplementedFailure({
133
+ phase: 'execute',
134
+ code: 'WORKFLOW_TOOL_EXECUTE_NOT_IMPLEMENTED',
135
+ message: `Workflow tool "${input.toolName}" has no execute implementation yet.`,
136
+ });
137
+ return {
138
+ receipt: receiptFromFailure(input.toolName, sessionState, index, notImplemented),
139
+ index,
140
+ sessionState,
141
+ };
142
+ }
143
+ let executionStep;
144
+ try {
145
+ executionStep = await input.hooks.execute(context, resolvedStep.value, planStep.value);
146
+ }
147
+ catch (error) {
148
+ executionStep = workflowStepFailure(exceptionFailure('execute', error));
149
+ }
150
+ if (!executionStep.ok) {
151
+ return {
152
+ receipt: receiptFromFailure(input.toolName, sessionState, index, executionStep.failure),
153
+ index,
154
+ sessionState,
155
+ };
156
+ }
157
+ if (input.hooks.verify == null) {
158
+ const notImplemented = workflowNotImplementedFailure({
159
+ phase: 'verify',
160
+ code: 'WORKFLOW_TOOL_VERIFY_NOT_IMPLEMENTED',
161
+ message: `Workflow tool "${input.toolName}" has no verify implementation yet.`,
162
+ });
163
+ return {
164
+ receipt: receiptFromFailure(input.toolName, sessionState, index, notImplemented),
165
+ index,
166
+ sessionState,
167
+ };
168
+ }
169
+ let verificationStep;
170
+ try {
171
+ verificationStep = await input.hooks.verify(context, resolvedStep.value, planStep.value, executionStep.value);
172
+ }
173
+ catch (error) {
174
+ verificationStep = workflowStepFailure(exceptionFailure('verify', error));
175
+ }
176
+ if (!verificationStep.ok) {
177
+ return {
178
+ receipt: receiptFromFailure(input.toolName, sessionState, index, verificationStep.failure),
179
+ index,
180
+ sessionState,
181
+ };
182
+ }
183
+ return {
184
+ receipt: receipt.createWorkflowSuccessReceipt({
185
+ toolName: input.toolName,
186
+ sessionKey: sessionState.documentKey,
187
+ message: `Workflow tool "${input.toolName}" completed.`,
188
+ index: toIndexSummary(index),
189
+ }),
190
+ output: {
191
+ resolved: resolvedStep.value,
192
+ plan: planStep.value,
193
+ execution: executionStep.value,
194
+ verification: verificationStep.value,
195
+ },
196
+ index,
197
+ sessionState,
198
+ };
199
+ }
200
+
201
+ exports.runWorkflowEngine = runWorkflowEngine;
202
+ exports.workflowNotImplementedFailure = workflowNotImplementedFailure;
203
+ exports.workflowStepFailure = workflowStepFailure;
204
+ exports.workflowStepSuccess = workflowStepSuccess;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Shared resolve -> plan -> execute -> verify pipeline for action-primitive tools.
3
+ *
4
+ * Each workflow tool plugs in only the steps it needs. The engine owns the
5
+ * common concerns: reuse the indexed document snapshot, convert thrown
6
+ * exceptions into structured receipts, and return a stable success/failure
7
+ * payload shape to the caller.
8
+ */
9
+ import type { BoundDocApi, DocInfoResult } from '../generated/client.js';
10
+ import type { InvokeOptions } from '../runtime/process.js';
11
+ import { type WorkflowDocIndex } from './doc-index.js';
12
+ import { type WorkflowExecutionPhase, type WorkflowToolResult } from './receipt.js';
13
+ import { type WorkflowSessionCache, type WorkflowSessionState } from './session-cache.js';
14
+ import type { WorkflowPocToolName } from './types.js';
15
+ export type WorkflowEngineFailure = {
16
+ status: 'failed' | 'not_implemented';
17
+ phase: WorkflowExecutionPhase;
18
+ code: string;
19
+ message: string;
20
+ details?: Record<string, unknown>;
21
+ };
22
+ export type WorkflowStepResult<TValue> = {
23
+ ok: true;
24
+ value: TValue;
25
+ } | {
26
+ ok: false;
27
+ failure: WorkflowEngineFailure;
28
+ };
29
+ export declare function workflowStepSuccess<TValue>(value: TValue): WorkflowStepResult<TValue>;
30
+ export declare function workflowStepFailure(failure: WorkflowEngineFailure): WorkflowStepResult<never>;
31
+ export declare function workflowNotImplementedFailure(input: {
32
+ phase: WorkflowExecutionPhase;
33
+ code: string;
34
+ message: string;
35
+ details?: Record<string, unknown>;
36
+ }): WorkflowEngineFailure;
37
+ export type WorkflowEngineContext = {
38
+ toolName: WorkflowPocToolName;
39
+ args: Record<string, unknown>;
40
+ documentHandle: BoundDocApi;
41
+ invokeOptions?: InvokeOptions;
42
+ sessionState: WorkflowSessionState;
43
+ info: DocInfoResult;
44
+ index: WorkflowDocIndex;
45
+ };
46
+ export type WorkflowEngineHooks<TResolved, TPlan, TExecution, TVerification> = {
47
+ resolve: (context: WorkflowEngineContext) => Promise<WorkflowStepResult<TResolved>>;
48
+ plan?: (context: WorkflowEngineContext, resolved: TResolved) => Promise<WorkflowStepResult<TPlan>>;
49
+ execute?: (context: WorkflowEngineContext, resolved: TResolved, plan: TPlan) => Promise<WorkflowStepResult<TExecution>>;
50
+ verify?: (context: WorkflowEngineContext, resolved: TResolved, plan: TPlan, execution: TExecution) => Promise<WorkflowStepResult<TVerification>>;
51
+ };
52
+ export type WorkflowEngineRunInput<TResolved, TPlan, TExecution, TVerification> = {
53
+ documentHandle: BoundDocApi;
54
+ toolName: WorkflowPocToolName;
55
+ args: Record<string, unknown>;
56
+ invokeOptions?: InvokeOptions;
57
+ cache?: WorkflowSessionCache;
58
+ hooks: WorkflowEngineHooks<TResolved, TPlan, TExecution, TVerification>;
59
+ };
60
+ export type WorkflowEngineOutput<TResolved, TPlan, TExecution, TVerification> = {
61
+ resolved: TResolved;
62
+ plan: TPlan;
63
+ execution: TExecution;
64
+ verification: TVerification;
65
+ };
66
+ export type WorkflowEngineRunResult<TResolved, TPlan, TExecution, TVerification> = WorkflowToolResult<WorkflowEngineOutput<TResolved, TPlan, TExecution, TVerification>> & {
67
+ index: WorkflowDocIndex;
68
+ sessionState: WorkflowSessionState;
69
+ };
70
+ export declare function runWorkflowEngine<TResolved, TPlan, TExecution, TVerification>(input: WorkflowEngineRunInput<TResolved, TPlan, TExecution, TVerification>): Promise<WorkflowEngineRunResult<TResolved, TPlan, TExecution, TVerification>>;
71
+ //# sourceMappingURL=engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/action-primitives/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAyB,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC9E,OAAO,EAIL,KAAK,sBAAsB,EAE3B,KAAK,kBAAkB,EACxB,MAAM,cAAc,CAAC;AACtB,OAAO,EAA2B,KAAK,oBAAoB,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACnH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtD,MAAM,MAAM,qBAAqB,GAAG;IAClC,MAAM,EAAE,QAAQ,GAAG,iBAAiB,CAAC;IACrC,KAAK,EAAE,sBAAsB,CAAC;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAA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F,wBAAsB,iBAAiB,CAAC,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,EACjF,KAAK,EAAE,sBAAsB,CAAC,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,CAAC,GACzE,OAAO,CAAC,uBAAuB,CAAC,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,CAwH/E"}
@@ -0,0 +1,196 @@
1
+ import { buildWorkflowDocIndex } from './doc-index.js';
2
+ import { createWorkflowFailureReceipt, createWorkflowNotImplementedReceipt, createWorkflowSuccessReceipt, } from './receipt.js';
3
+ import { workflowPocSessionCache } from './session-cache.js';
4
+ export function workflowStepSuccess(value) {
5
+ return { ok: true, value };
6
+ }
7
+ export function workflowStepFailure(failure) {
8
+ return { ok: false, failure };
9
+ }
10
+ export function workflowNotImplementedFailure(input) {
11
+ return {
12
+ status: 'not_implemented',
13
+ phase: input.phase,
14
+ code: input.code,
15
+ message: input.message,
16
+ details: input.details,
17
+ };
18
+ }
19
+ function toIndexSummary(index) {
20
+ return {
21
+ revision: index.revision,
22
+ blocks: index.blocks.length,
23
+ lists: index.lists.length,
24
+ tables: index.tables.length,
25
+ };
26
+ }
27
+ function coerceErrorMessage(error) {
28
+ if (error instanceof Error) {
29
+ return error.message;
30
+ }
31
+ return String(error);
32
+ }
33
+ async function ensureDocIndex(input) {
34
+ const sessionState = input.cache.getState(input.documentHandle);
35
+ const info = await input.documentHandle.info({}, input.invokeOptions);
36
+ const cached = input.cache.getCachedIndex(input.documentHandle, info.revision);
37
+ if (cached != null) {
38
+ return { sessionState, info, index: cached };
39
+ }
40
+ const index = await buildWorkflowDocIndex({
41
+ documentHandle: input.documentHandle,
42
+ documentKey: sessionState.documentKey,
43
+ invokeOptions: input.invokeOptions,
44
+ info,
45
+ });
46
+ input.cache.setCachedIndex(input.documentHandle, index);
47
+ return { sessionState, info, index };
48
+ }
49
+ function receiptFromFailure(toolName, sessionState, index, failure) {
50
+ const details = { code: failure.code, ...failure.details };
51
+ if (failure.status === 'not_implemented') {
52
+ return createWorkflowNotImplementedReceipt({
53
+ toolName,
54
+ sessionKey: sessionState.documentKey,
55
+ phase: failure.phase,
56
+ message: failure.message,
57
+ index: toIndexSummary(index),
58
+ details,
59
+ });
60
+ }
61
+ return createWorkflowFailureReceipt({
62
+ toolName,
63
+ sessionKey: sessionState.documentKey,
64
+ phase: failure.phase,
65
+ message: failure.message,
66
+ index: toIndexSummary(index),
67
+ details,
68
+ });
69
+ }
70
+ function exceptionFailure(phase, error) {
71
+ return {
72
+ status: 'failed',
73
+ phase,
74
+ code: 'WORKFLOW_ENGINE_STEP_EXCEPTION',
75
+ message: `Workflow engine phase "${phase}" threw unexpectedly.`,
76
+ details: { error: coerceErrorMessage(error) },
77
+ };
78
+ }
79
+ export async function runWorkflowEngine(input) {
80
+ const cache = input.cache ?? workflowPocSessionCache;
81
+ const { sessionState, info, index } = await ensureDocIndex({
82
+ documentHandle: input.documentHandle,
83
+ invokeOptions: input.invokeOptions,
84
+ cache,
85
+ });
86
+ const context = {
87
+ toolName: input.toolName,
88
+ args: input.args,
89
+ documentHandle: input.documentHandle,
90
+ invokeOptions: input.invokeOptions,
91
+ sessionState,
92
+ info,
93
+ index,
94
+ };
95
+ let resolvedStep;
96
+ try {
97
+ resolvedStep = await input.hooks.resolve(context);
98
+ }
99
+ catch (error) {
100
+ resolvedStep = workflowStepFailure(exceptionFailure('resolve', error));
101
+ }
102
+ if (!resolvedStep.ok) {
103
+ return {
104
+ receipt: receiptFromFailure(input.toolName, sessionState, index, resolvedStep.failure),
105
+ index,
106
+ sessionState,
107
+ };
108
+ }
109
+ let planStep;
110
+ try {
111
+ if (input.hooks.plan == null) {
112
+ planStep = workflowStepSuccess({ resolved: resolvedStep.value, args: context.args });
113
+ }
114
+ else {
115
+ planStep = await input.hooks.plan(context, resolvedStep.value);
116
+ }
117
+ }
118
+ catch (error) {
119
+ planStep = workflowStepFailure(exceptionFailure('plan', error));
120
+ }
121
+ if (!planStep.ok) {
122
+ return {
123
+ receipt: receiptFromFailure(input.toolName, sessionState, index, planStep.failure),
124
+ index,
125
+ sessionState,
126
+ };
127
+ }
128
+ if (input.hooks.execute == null) {
129
+ const notImplemented = workflowNotImplementedFailure({
130
+ phase: 'execute',
131
+ code: 'WORKFLOW_TOOL_EXECUTE_NOT_IMPLEMENTED',
132
+ message: `Workflow tool "${input.toolName}" has no execute implementation yet.`,
133
+ });
134
+ return {
135
+ receipt: receiptFromFailure(input.toolName, sessionState, index, notImplemented),
136
+ index,
137
+ sessionState,
138
+ };
139
+ }
140
+ let executionStep;
141
+ try {
142
+ executionStep = await input.hooks.execute(context, resolvedStep.value, planStep.value);
143
+ }
144
+ catch (error) {
145
+ executionStep = workflowStepFailure(exceptionFailure('execute', error));
146
+ }
147
+ if (!executionStep.ok) {
148
+ return {
149
+ receipt: receiptFromFailure(input.toolName, sessionState, index, executionStep.failure),
150
+ index,
151
+ sessionState,
152
+ };
153
+ }
154
+ if (input.hooks.verify == null) {
155
+ const notImplemented = workflowNotImplementedFailure({
156
+ phase: 'verify',
157
+ code: 'WORKFLOW_TOOL_VERIFY_NOT_IMPLEMENTED',
158
+ message: `Workflow tool "${input.toolName}" has no verify implementation yet.`,
159
+ });
160
+ return {
161
+ receipt: receiptFromFailure(input.toolName, sessionState, index, notImplemented),
162
+ index,
163
+ sessionState,
164
+ };
165
+ }
166
+ let verificationStep;
167
+ try {
168
+ verificationStep = await input.hooks.verify(context, resolvedStep.value, planStep.value, executionStep.value);
169
+ }
170
+ catch (error) {
171
+ verificationStep = workflowStepFailure(exceptionFailure('verify', error));
172
+ }
173
+ if (!verificationStep.ok) {
174
+ return {
175
+ receipt: receiptFromFailure(input.toolName, sessionState, index, verificationStep.failure),
176
+ index,
177
+ sessionState,
178
+ };
179
+ }
180
+ return {
181
+ receipt: createWorkflowSuccessReceipt({
182
+ toolName: input.toolName,
183
+ sessionKey: sessionState.documentKey,
184
+ message: `Workflow tool "${input.toolName}" completed.`,
185
+ index: toIndexSummary(index),
186
+ }),
187
+ output: {
188
+ resolved: resolvedStep.value,
189
+ plan: planStep.value,
190
+ execution: executionStep.value,
191
+ verification: verificationStep.value,
192
+ },
193
+ index,
194
+ sessionState,
195
+ };
196
+ }
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const WORKFLOW_POC_PROFILE = 'workflow-poc';
4
+ function createWorkflowReceiptBase(input) {
5
+ return {
6
+ profile: WORKFLOW_POC_PROFILE,
7
+ toolName: input.toolName,
8
+ status: input.status,
9
+ phase: input.phase,
10
+ sessionKey: input.sessionKey,
11
+ message: input.message,
12
+ index: input.index,
13
+ details: input.details,
14
+ };
15
+ }
16
+ function createWorkflowSuccessReceipt(input) {
17
+ return createWorkflowReceiptBase({
18
+ ...input,
19
+ phase: input.phase ?? 'verify',
20
+ status: 'success',
21
+ });
22
+ }
23
+ function createWorkflowFailureReceipt(input) {
24
+ return createWorkflowReceiptBase({
25
+ ...input,
26
+ status: 'failed',
27
+ });
28
+ }
29
+ function createWorkflowNotImplementedReceipt(input) {
30
+ return createWorkflowReceiptBase({
31
+ ...input,
32
+ status: 'not_implemented',
33
+ });
34
+ }
35
+
36
+ exports.WORKFLOW_POC_PROFILE = WORKFLOW_POC_PROFILE;
37
+ exports.createWorkflowFailureReceipt = createWorkflowFailureReceipt;
38
+ exports.createWorkflowNotImplementedReceipt = createWorkflowNotImplementedReceipt;
39
+ exports.createWorkflowSuccessReceipt = createWorkflowSuccessReceipt;
@@ -0,0 +1,49 @@
1
+ import type { WorkflowPocToolName } from './types.js';
2
+ export declare const WORKFLOW_POC_PROFILE: "workflow-poc";
3
+ export type WorkflowExecutionPhase = 'resolve' | 'plan' | 'execute' | 'verify';
4
+ export type WorkflowReceiptStatus = 'success' | 'failed' | 'not_implemented';
5
+ export type WorkflowIndexSummary = {
6
+ revision: string;
7
+ blocks: number;
8
+ lists: number;
9
+ tables: number;
10
+ };
11
+ export type WorkflowReceipt = {
12
+ profile: typeof WORKFLOW_POC_PROFILE;
13
+ toolName: WorkflowPocToolName;
14
+ status: WorkflowReceiptStatus;
15
+ phase: WorkflowExecutionPhase;
16
+ sessionKey: string;
17
+ message: string;
18
+ index: WorkflowIndexSummary;
19
+ details?: Record<string, unknown>;
20
+ };
21
+ export type WorkflowToolResult<TOutput = unknown> = {
22
+ receipt: WorkflowReceipt;
23
+ output?: TOutput;
24
+ };
25
+ export declare function createWorkflowSuccessReceipt(input: {
26
+ toolName: WorkflowPocToolName;
27
+ sessionKey: string;
28
+ phase?: WorkflowExecutionPhase;
29
+ message: string;
30
+ index: WorkflowIndexSummary;
31
+ details?: Record<string, unknown>;
32
+ }): WorkflowReceipt;
33
+ export declare function createWorkflowFailureReceipt(input: {
34
+ toolName: WorkflowPocToolName;
35
+ sessionKey: string;
36
+ phase: WorkflowExecutionPhase;
37
+ message: string;
38
+ index: WorkflowIndexSummary;
39
+ details?: Record<string, unknown>;
40
+ }): WorkflowReceipt;
41
+ export declare function createWorkflowNotImplementedReceipt(input: {
42
+ toolName: WorkflowPocToolName;
43
+ sessionKey: string;
44
+ phase: WorkflowExecutionPhase;
45
+ message: string;
46
+ index: WorkflowIndexSummary;
47
+ details?: Record<string, unknown>;
48
+ }): WorkflowReceipt;
49
+ //# sourceMappingURL=receipt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"receipt.d.ts","sourceRoot":"","sources":["../../src/action-primitives/receipt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtD,eAAO,MAAM,oBAAoB,EAAG,cAAuB,CAAC;AAE5D,MAAM,MAAM,sBAAsB,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;AAC/E,MAAM,MAAM,qBAAqB,GAAG,SAAS,GAAG,QAAQ,GAAG,iBAAiB,CAAC;AAE7E,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,M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9B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,sBAAsB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,oBAAoB,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,GAAG,eAAe,CAKlB"}
@@ -0,0 +1,32 @@
1
+ export const WORKFLOW_POC_PROFILE = 'workflow-poc';
2
+ function createWorkflowReceiptBase(input) {
3
+ return {
4
+ profile: WORKFLOW_POC_PROFILE,
5
+ toolName: input.toolName,
6
+ status: input.status,
7
+ phase: input.phase,
8
+ sessionKey: input.sessionKey,
9
+ message: input.message,
10
+ index: input.index,
11
+ details: input.details,
12
+ };
13
+ }
14
+ export function createWorkflowSuccessReceipt(input) {
15
+ return createWorkflowReceiptBase({
16
+ ...input,
17
+ phase: input.phase ?? 'verify',
18
+ status: 'success',
19
+ });
20
+ }
21
+ export function createWorkflowFailureReceipt(input) {
22
+ return createWorkflowReceiptBase({
23
+ ...input,
24
+ status: 'failed',
25
+ });
26
+ }
27
+ export function createWorkflowNotImplementedReceipt(input) {
28
+ return createWorkflowReceiptBase({
29
+ ...input,
30
+ status: 'not_implemented',
31
+ });
32
+ }