MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / policiesCreate

Function policiesCreate

packages/core/sdk/src/executor.ts:5850–5898  ·  view source on GitHub ↗
(
      input: CreateToolPolicyInput,
    )

Source from the content-addressed store, hash-verified

5848 );
5849
5850 const policiesCreate = (
5851 input: CreateToolPolicyInput,
5852 ): Effect.Effect<ToolPolicy, OrgWriteDeniedError | StorageFailure> =>
5853 transaction(
5854 Effect.gen(function* () {
5855 yield* guardOrgWrite(input.owner);
5856 if (!isValidPattern(input.pattern)) {
5857 return yield* new StorageError({
5858 message: `Invalid tool policy pattern: ${input.pattern}`,
5859 cause: undefined,
5860 });
5861 }
5862 if (!isToolPolicyAction(input.action)) {
5863 return yield* new StorageError({
5864 message: `Invalid tool policy action: ${String(input.action)}`,
5865 cause: undefined,
5866 });
5867 }
5868 yield* requireUserSubject(input.owner);
5869 const keys = yield* Effect.try({
5870 try: () => ownedKeys(input.owner),
5871 catch: (cause) => storageFailureFromUnknown("invalid owner", cause),
5872 });
5873 const existing = yield* core.findMany("tool_policy", {
5874 where: byOwner(input.owner),
5875 });
5876 // Default placement is specificity-aware (below any more-specific
5877 // rule), not top-of-list: a client that omits position — the UI when
5878 // its policy list is stale, the API, an agent tool — must not have its
5879 // broad rule silently shadow an existing narrow one.
5880 const position = input.position ?? positionForNewPattern(input.pattern, existing);
5881 const id = PolicyId.make(
5882 `pol_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`,
5883 );
5884 const now = new Date();
5885 const created = yield* core.create("tool_policy", {
5886 tenant: keys.tenant,
5887 owner: keys.owner,
5888 subject: keys.subject,
5889 id: String(id),
5890 pattern: input.pattern,
5891 action: input.action,
5892 position,
5893 created_at: now,
5894 updated_at: now,
5895 });
5896 return rowToToolPolicy(created);
5897 }),
5898 );
5899
5900 const policiesUpdate = (
5901 input: UpdateToolPolicyInput,

Callers 1

createExecutorFunction · 0.85

Calls 11

isValidPatternFunction · 0.90
isToolPolicyActionFunction · 0.90
positionForNewPatternFunction · 0.90
rowToToolPolicyFunction · 0.90
guardOrgWriteFunction · 0.85
requireUserSubjectFunction · 0.85
ownedKeysFunction · 0.85
byOwnerFunction · 0.85
toStringMethod · 0.80
transactionFunction · 0.70

Tested by

no test coverage detected