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

Function policiesCreate

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

Source from the content-addressed store, hash-verified

5967 );
5968
5969 const policiesCreate = (
5970 input: CreateToolPolicyInput,
5971 ): Effect.Effect<ToolPolicy, OrgWriteDeniedError | StorageFailure> =>
5972 transaction(
5973 Effect.gen(function* () {
5974 yield* guardOrgWrite(input.owner);
5975 if (!isValidPattern(input.pattern)) {
5976 return yield* new StorageError({
5977 message: `Invalid tool policy pattern: ${input.pattern}`,
5978 cause: undefined,
5979 });
5980 }
5981 if (!isToolPolicyAction(input.action)) {
5982 return yield* new StorageError({
5983 message: `Invalid tool policy action: ${String(input.action)}`,
5984 cause: undefined,
5985 });
5986 }
5987 yield* requireUserSubject(input.owner);
5988 const keys = yield* Effect.try({
5989 try: () => ownedKeys(input.owner),
5990 catch: (cause) => storageFailureFromUnknown("invalid owner", cause),
5991 });
5992 const existing = yield* core.findMany("tool_policy", {
5993 where: byOwner(input.owner),
5994 });
5995 // Default placement is specificity-aware (below any more-specific
5996 // rule), not top-of-list: a client that omits position — the UI when
5997 // its policy list is stale, the API, an agent tool — must not have its
5998 // broad rule silently shadow an existing narrow one.
5999 const position = input.position ?? positionForNewPattern(input.pattern, existing);
6000 const id = PolicyId.make(
6001 `pol_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`,
6002 );
6003 const now = new Date();
6004 const created = yield* core.create("tool_policy", {
6005 tenant: keys.tenant,
6006 owner: keys.owner,
6007 subject: keys.subject,
6008 id: String(id),
6009 pattern: input.pattern,
6010 action: input.action,
6011 position,
6012 created_at: now,
6013 updated_at: now,
6014 });
6015 return rowToToolPolicy(created);
6016 }),
6017 );
6018
6019 const policiesUpdate = (
6020 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