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

Function policiesCreate

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

Source from the content-addressed store, hash-verified

6059 );
6060
6061 const policiesCreate = (
6062 input: CreateToolPolicyInput,
6063 ): Effect.Effect<ToolPolicy, OrgWriteDeniedError | StorageFailure> =>
6064 transaction(
6065 Effect.gen(function* () {
6066 yield* guardOrgWrite(input.owner);
6067 if (!isValidPattern(input.pattern)) {
6068 return yield* new StorageError({
6069 message: `Invalid tool policy pattern: ${input.pattern}`,
6070 cause: undefined,
6071 });
6072 }
6073 if (!isToolPolicyAction(input.action)) {
6074 return yield* new StorageError({
6075 message: `Invalid tool policy action: ${String(input.action)}`,
6076 cause: undefined,
6077 });
6078 }
6079 yield* requireUserSubject(input.owner);
6080 const keys = yield* Effect.try({
6081 try: () => ownedKeys(input.owner),
6082 catch: (cause) => storageFailureFromUnknown("invalid owner", cause),
6083 });
6084 const existing = yield* core.findMany("tool_policy", {
6085 where: byOwner(input.owner),
6086 });
6087 // Default placement is specificity-aware (below any more-specific
6088 // rule), not top-of-list: a client that omits position — the UI when
6089 // its policy list is stale, the API, an agent tool — must not have its
6090 // broad rule silently shadow an existing narrow one.
6091 const position = input.position ?? positionForNewPattern(input.pattern, existing);
6092 const id = PolicyId.make(
6093 `pol_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`,
6094 );
6095 const now = new Date();
6096 const created = yield* core.create("tool_policy", {
6097 tenant: keys.tenant,
6098 owner: keys.owner,
6099 subject: keys.subject,
6100 id: String(id),
6101 pattern: input.pattern,
6102 action: input.action,
6103 position,
6104 created_at: now,
6105 updated_at: now,
6106 });
6107 return rowToToolPolicy(created);
6108 }),
6109 );
6110
6111 const policiesUpdate = (
6112 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