@ankhorage/projects 0.9.3 → 0.9.4

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.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # Ankhorage Projects
5
5
 
6
- ![license: MIT](././paradox/badges/license.svg) ![npm: v0.9.3](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: configured](././paradox/badges/tests.svg) ![paradox: canonical](././paradox/badges/docs.svg)
6
+ ![license: MIT](././paradox/badges/license.svg) ![npm: v0.9.4](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: configured](././paradox/badges/tests.svg) ![paradox: canonical](././paradox/badges/docs.svg)
7
7
 
8
8
  Provides standalone project identity, persistence, and repository-oriented project actions.
9
9
 
@@ -1,9 +1,9 @@
1
- import { createHash } from 'node:crypto';
2
1
  import { createFlowNodeOperationResolver, createFlowRuntime, } from '@ankhorage/flow';
3
2
  import { executeSocialActionAsync, } from '@ankhorage/social';
4
3
  import { createKeyedAsyncMutex } from '@ankhorage/utility/concurrency';
5
- import { assertCallerScope, readPublicationConfig, sameTarget, } from '../domain/publicationConfig.js';
6
- import { readDestinationAsync, requireBindingAsync, resolveCommandAsync, } from './resolveSocialManagerPublication.js';
4
+ import { readPublicationConfig, sameTarget } from '../domain/publicationConfig.js';
5
+ import { resolveCommandAsync } from './resolveSocialManagerPublication.js';
6
+ import { scheduleSocialManagerPublicationAsync } from './scheduleSocialManagerPublicationAsync.js';
7
7
  const capabilityId = 'projects.social.publication.execute';
8
8
  const providerId = '@ankhorage/projects';
9
9
  /*** Compose the released Flow engine with one protected Social operation and immutable draft references. */
@@ -28,126 +28,7 @@ export function createSocialManagerPublicationRuntime(input) {
28
28
  });
29
29
  return {
30
30
  runtime,
31
- scheduleAsync: (request) => mutex.run(publicationId(request), () => scheduleAsync(input, runtime, request)),
32
- };
33
- }
34
- /*** Persist one exact, independently approvable destination run; Flow owns scheduling and execution. */
35
- async function scheduleAsync(input, runtime, request) {
36
- const scheduledRequest = {
37
- ...request,
38
- runAt: request.runAt ?? (input.flow.clock?.now() ?? new Date()).toISOString(),
39
- };
40
- const { caller, draftId, revision, destinationIndex, runAt } = scheduledRequest;
41
- assertCallerScope(input.scope, caller);
42
- assertScheduleInput(scheduledRequest);
43
- const destination = await readDestinationAsync(input, scheduledRequest);
44
- const binding = await requireBindingAsync(input, caller, destination);
45
- const config = {
46
- caller,
47
- draftId,
48
- revision,
49
- destinationIndex,
50
- runAt,
51
- connectionRevision: binding.connection.revision,
52
- target: binding.target,
53
- };
54
- const flow = createPublicationDefinition(config);
55
- const scope = { tenantId: caller.tenantId, projectId: caller.projectId };
56
- const existing = await runtime.getWorkflowAsync(scope, flow.id, flow.version);
57
- if (!existing)
58
- await runtime.createDraftAsync(scope, flow);
59
- else if (JSON.stringify(existing.definition) !== JSON.stringify(flow))
60
- throw new Error('Social publication configuration changed.');
61
- if (!existing?.publishedAt)
62
- await runtime.publishAsync(scope, flow.id, flow.version);
63
- const run = await runtime.triggerAsync({
64
- scope,
65
- flowId: flow.id,
66
- triggerId: 'publish',
67
- idempotencyKey: flow.id,
68
- runAt,
69
- });
70
- const approvalId = await ensureApprovalAsync(input, runtime, scheduledRequest, run.id);
71
- return { run, approvalId };
72
- }
73
- /*** Create a pending approval only once, without resetting a previously resolved decision. */
74
- async function ensureApprovalAsync(input, runtime, request, runId) {
75
- const { caller } = request;
76
- const scope = { tenantId: caller.tenantId, projectId: caller.projectId };
77
- const approvalId = `social-approval:${runId}`;
78
- if (!(await input.flow.store.getApprovalAsync(scope, approvalId)))
79
- await runtime.requestApprovalAsync({
80
- scope,
81
- approval: {
82
- id: approvalId,
83
- runId,
84
- status: 'pending',
85
- requestedBy: caller.actorId,
86
- requestedAt: (input.flow.clock?.now() ?? new Date()).toISOString(),
87
- },
88
- });
89
- return approvalId;
90
- }
91
- /*** Validate one exact destination revision and canonical UTC instant. */
92
- function assertScheduleInput(request) {
93
- const { caller, revision, destinationIndex, runAt } = request;
94
- if (!caller.actorId.trim())
95
- throw new Error('Social publication requires a caller.');
96
- if (!Number.isSafeInteger(revision) ||
97
- revision < 1 ||
98
- !Number.isSafeInteger(destinationIndex) ||
99
- destinationIndex < 0 ||
100
- (runAt !== undefined &&
101
- (!Number.isFinite(Date.parse(runAt)) || new Date(runAt).toISOString() !== runAt))) {
102
- throw new Error('Invalid Social Manager publication request.');
103
- }
104
- }
105
- /*** Derive a stable intent identity so a repeated schedule request cannot create another run. */
106
- function publicationId(request) {
107
- const { caller, draftId, revision, destinationIndex, runAt } = request;
108
- return ('social-' +
109
- createHash('sha256')
110
- .update(JSON.stringify([
111
- caller.tenantId,
112
- caller.projectId,
113
- caller.environmentId,
114
- caller.actorId,
115
- draftId,
116
- revision,
117
- destinationIndex,
118
- runAt ?? null,
119
- ]))
120
- .digest('hex'));
121
- }
122
- /*** Construct a server-owned graph that pins the approved revision and provider destination. */
123
- function createPublicationDefinition(config) {
124
- return {
125
- id: publicationId(config),
126
- version: 1,
127
- title: 'Social publication',
128
- triggers: [{ id: 'publish', kind: 'manual' }],
129
- edges: [],
130
- nodes: [
131
- {
132
- id: 'publication',
133
- capability: { providerId, capabilityId },
134
- config: {
135
- publication: {
136
- kind: 'inline',
137
- value: {
138
- caller: { ...config.caller },
139
- draftId: config.draftId,
140
- revision: config.revision,
141
- destinationIndex: config.destinationIndex,
142
- runAt: config.runAt,
143
- connectionRevision: config.connectionRevision,
144
- target: { ...config.target },
145
- },
146
- },
147
- },
148
- ports: [{ id: 'receipt', direction: 'output' }],
149
- },
150
- ],
31
+ scheduleAsync: (request) => scheduleSocialManagerPublicationAsync(input, runtime, mutex, request),
151
32
  };
152
33
  }
153
34
  /*** Register an externally effective node; no internal operation can bypass the Flow security permit. */
@@ -203,8 +84,7 @@ async function requireApprovalAsync(input, node, operationKey) {
203
84
  const scope = { tenantId: config.caller.tenantId, projectId: config.caller.projectId };
204
85
  const run = await input.flow.store.getRunAsync(scope, runId);
205
86
  const approval = await input.flow.store.getApprovalAsync(scope, 'social-approval:' + runId);
206
- if (run?.flow.id !== publicationId(config) ||
207
- run.flow.version !== 1 ||
87
+ if (run?.flow.version !== 1 ||
208
88
  approval?.runId !== runId ||
209
89
  approval.status !== 'approved' ||
210
90
  (approval.expiresAt !== undefined &&
@@ -0,0 +1,8 @@
1
+ import type { FlowRuntime } from '@ankhorage/flow';
2
+ import type { KeyedAsyncMutex } from '@ankhorage/utility/concurrency';
3
+ import type { SocialManagerPublicationDependencies, SocialManagerPublicationRequest } from '../../../types/socialManagerPublication.js';
4
+ /*** Persist one exact, independently approvable destination run; Flow owns scheduling and execution. */
5
+ export declare function scheduleSocialManagerPublicationAsync(input: SocialManagerPublicationDependencies, runtime: FlowRuntime, mutex: KeyedAsyncMutex<string>, request: SocialManagerPublicationRequest): Promise<{
6
+ readonly run: Awaited<ReturnType<FlowRuntime['triggerAsync']>>;
7
+ readonly approvalId: string;
8
+ }>;
@@ -0,0 +1,118 @@
1
+ import { assertCallerScope, readPublicationConfig, } from '../domain/publicationConfig.js';
2
+ import { readDestinationAsync, requireBindingAsync } from './resolveSocialManagerPublication.js';
3
+ import { socialManagerPublicationIdentity } from './socialManagerPublicationIdentity.js';
4
+ const capabilityId = 'projects.social.publication.execute';
5
+ const providerId = '@ankhorage/projects';
6
+ /*** Persist one exact, independently approvable destination run; Flow owns scheduling and execution. */
7
+ export async function scheduleSocialManagerPublicationAsync(input, runtime, mutex, request) {
8
+ return mutex.run(socialManagerPublicationIdentity(request), async () => {
9
+ const scope = { tenantId: request.caller.tenantId, projectId: request.caller.projectId };
10
+ const id = socialManagerPublicationIdentity(request);
11
+ const existing = await runtime.getWorkflowAsync(scope, id, 1);
12
+ const scheduledRequest = {
13
+ ...request,
14
+ runAt: request.runAt ?? existingRunAt(existing, input.scope) ?? currentTime(input),
15
+ };
16
+ const { caller, draftId, revision, destinationIndex, runAt } = scheduledRequest;
17
+ assertCallerScope(input.scope, caller);
18
+ assertScheduleInput(scheduledRequest);
19
+ const destination = await readDestinationAsync(input, scheduledRequest);
20
+ const binding = await requireBindingAsync(input, caller, destination);
21
+ const config = {
22
+ caller,
23
+ draftId,
24
+ revision,
25
+ destinationIndex,
26
+ runAt,
27
+ connectionRevision: binding.connection.revision,
28
+ target: binding.target,
29
+ };
30
+ const flow = createPublicationDefinition(config, id);
31
+ if (!existing)
32
+ await runtime.createDraftAsync(scope, flow);
33
+ else if (JSON.stringify(existing.definition) !== JSON.stringify(flow))
34
+ throw new Error('Social publication configuration changed.');
35
+ if (!existing?.publishedAt)
36
+ await runtime.publishAsync(scope, flow.id, flow.version);
37
+ const run = await runtime.triggerAsync({
38
+ scope,
39
+ flowId: flow.id,
40
+ triggerId: 'publish',
41
+ idempotencyKey: flow.id,
42
+ runAt,
43
+ });
44
+ const approvalId = await ensureApprovalAsync(input, runtime, scheduledRequest, run.id);
45
+ return { run, approvalId };
46
+ });
47
+ }
48
+ /*** Reuse the original immediate-run timestamp so an identical retry retains its immutable Flow definition. */
49
+ function existingRunAt(existing, scope) {
50
+ const node = existing?.definition.nodes.find((candidate) => candidate.id === 'publication');
51
+ return node === undefined ? undefined : readPublicationConfig(node, scope).runAt;
52
+ }
53
+ /*** Read the single clock boundary used when an immediate publication first becomes a scheduled Flow run. */
54
+ function currentTime(input) {
55
+ return (input.flow.clock?.now() ?? new Date()).toISOString();
56
+ }
57
+ /*** Create a pending approval only once, without resetting a previously resolved decision. */
58
+ async function ensureApprovalAsync(input, runtime, request, runId) {
59
+ const { caller } = request;
60
+ const scope = { tenantId: caller.tenantId, projectId: caller.projectId };
61
+ const approvalId = `social-approval:${runId}`;
62
+ if (!(await input.flow.store.getApprovalAsync(scope, approvalId)))
63
+ await runtime.requestApprovalAsync({
64
+ scope,
65
+ approval: {
66
+ id: approvalId,
67
+ runId,
68
+ status: 'pending',
69
+ requestedBy: caller.actorId,
70
+ requestedAt: currentTime(input),
71
+ },
72
+ });
73
+ return approvalId;
74
+ }
75
+ /*** Validate one exact destination revision and canonical UTC instant. */
76
+ function assertScheduleInput(request) {
77
+ const { caller, revision, destinationIndex, runAt } = request;
78
+ if (!caller.actorId.trim())
79
+ throw new Error('Social publication requires a caller.');
80
+ if (!Number.isSafeInteger(revision) ||
81
+ revision < 1 ||
82
+ !Number.isSafeInteger(destinationIndex) ||
83
+ destinationIndex < 0 ||
84
+ (runAt !== undefined &&
85
+ (!Number.isFinite(Date.parse(runAt)) || new Date(runAt).toISOString() !== runAt)))
86
+ throw new Error('Invalid Social Manager publication request.');
87
+ }
88
+ /*** Construct a server-owned graph that pins the approved revision and provider destination. */
89
+ function createPublicationDefinition(config, id) {
90
+ return {
91
+ id,
92
+ version: 1,
93
+ title: 'Social publication',
94
+ triggers: [{ id: 'publish', kind: 'manual' }],
95
+ edges: [],
96
+ nodes: [
97
+ {
98
+ id: 'publication',
99
+ capability: { providerId, capabilityId },
100
+ config: {
101
+ publication: {
102
+ kind: 'inline',
103
+ value: {
104
+ caller: { ...config.caller },
105
+ draftId: config.draftId,
106
+ revision: config.revision,
107
+ destinationIndex: config.destinationIndex,
108
+ runAt: config.runAt,
109
+ connectionRevision: config.connectionRevision,
110
+ target: { ...config.target },
111
+ },
112
+ },
113
+ },
114
+ ports: [{ id: 'receipt', direction: 'output' }],
115
+ },
116
+ ],
117
+ };
118
+ }
@@ -0,0 +1,3 @@
1
+ import type { SocialManagerPublicationRequest } from '../../../types/socialManagerPublication.js';
2
+ /*** Derive the stable Flow identity for one immutable Social publication intent. */
3
+ export declare function socialManagerPublicationIdentity(request: SocialManagerPublicationRequest): string;
@@ -0,0 +1,18 @@
1
+ import { createHash } from 'node:crypto';
2
+ /*** Derive the stable Flow identity for one immutable Social publication intent. */
3
+ export function socialManagerPublicationIdentity(request) {
4
+ const { caller, draftId, revision, destinationIndex, runAt } = request;
5
+ return ('social-' +
6
+ createHash('sha256')
7
+ .update(JSON.stringify([
8
+ caller.tenantId,
9
+ caller.projectId,
10
+ caller.environmentId,
11
+ caller.actorId,
12
+ draftId,
13
+ revision,
14
+ destinationIndex,
15
+ runAt ?? null,
16
+ ]))
17
+ .digest('hex'));
18
+ }
@@ -194,7 +194,7 @@ function validateDestination(capability, destination, media) {
194
194
  /*** Verify that the exact draft's text, link, and media content classes are enabled by this provider target. */
195
195
  function contentTypeErrors(capability, destination, media) {
196
196
  const required = [
197
- ...(destination.text.trim() ? ['text'] : []),
197
+ ...(destination.text.trim() && media.length === 0 ? ['text'] : []),
198
198
  ...(destination.link === undefined ? [] : ['link']),
199
199
  ...media.map((item) => item.kind),
200
200
  ];
@@ -2,6 +2,7 @@ import { join } from 'node:path';
2
2
  import { FileFlowPersistenceAdapter, FileFlowRuntimeStore } from '@ankhorage/flow';
3
3
  import { createSocialManagerMediaPreflight } from '../adapters/createSocialManagerMediaPreflight.js';
4
4
  import { createTrustedSocialManagerHostAsync } from './createTrustedSocialManagerHost.js';
5
+ import { targetForConnection } from './targetForConnection.js';
5
6
  /*** Compose a local host from the durable stores and deterministic provider fixtures selected by the loopback server. */
6
7
  export async function createTrustedSocialManagerLocalHostAsync({ input, persistence, fixtures, now, }) {
7
8
  const resolveBindingAsync = createBindingResolver(persistence.connection, fixtures, input.caller);
@@ -98,28 +99,6 @@ function createMediaResolver(media) {
98
99
  return items.flatMap((item) => (item === undefined ? [] : [item.media]));
99
100
  };
100
101
  }
101
- /*** Verify the durable connection owner and map its selected authorized target to the published provider vocabulary. */
102
- function targetForConnection(connection, targetId) {
103
- const target = connection.targets.find((candidate) => candidate.targetId === targetId);
104
- if (!target || connection.status !== 'connected')
105
- return undefined;
106
- const base = { accountId: connection.accountId, destinationId: target.targetId };
107
- switch (connection.provider) {
108
- case 'facebook-login':
109
- return { ...base, provider: 'facebook', accountType: 'page', destinationType: 'page' };
110
- case 'instagram-login':
111
- return {
112
- ...base,
113
- provider: 'instagram',
114
- accountType: 'business',
115
- destinationType: 'account',
116
- };
117
- case 'linkedin':
118
- return { ...base, provider: 'linkedin', accountType: 'member', destinationType: 'member' };
119
- case 'pinterest':
120
- return { ...base, provider: 'pinterest', accountType: 'business', destinationType: 'board' };
121
- }
122
- }
123
102
  /*** Select exactly one local provider fixture from the server-resolved target provider. */
124
103
  function providerFor(fixtures, target) {
125
104
  switch (target.provider) {
@@ -0,0 +1,4 @@
1
+ import type { ManagedConnection } from '@ankhorage/connection';
2
+ import type { SocialTargetRef } from '@ankhorage/social';
3
+ /*** Map one connected, provider-authorized target to the published Social provider vocabulary. */
4
+ export declare function targetForConnection(connection: ManagedConnection, targetId: string): SocialTargetRef | undefined;
@@ -0,0 +1,27 @@
1
+ /*** Map one connected, provider-authorized target to the published Social provider vocabulary. */
2
+ export function targetForConnection(connection, targetId) {
3
+ const target = connection.targets.find((candidate) => candidate.targetId === targetId);
4
+ if (!target || connection.status !== 'connected')
5
+ return undefined;
6
+ const base = { accountId: connection.accountId, destinationId: target.targetId };
7
+ switch (connection.provider) {
8
+ case 'facebook-login':
9
+ return { ...base, provider: 'facebook', accountType: 'page', destinationType: 'page' };
10
+ case 'instagram-login':
11
+ return {
12
+ ...base,
13
+ provider: 'instagram',
14
+ accountType: 'business',
15
+ destinationType: 'instagram-account',
16
+ };
17
+ case 'linkedin':
18
+ return { ...base, provider: 'linkedin', accountType: 'member', destinationType: 'member' };
19
+ case 'pinterest':
20
+ return {
21
+ ...base,
22
+ provider: 'pinterest',
23
+ accountType: 'pinterest-user',
24
+ destinationType: 'board',
25
+ };
26
+ }
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ankhorage/projects",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Standalone project identity and repository actions.",
5
5
  "keywords": [
6
6
  "ankhorage",