@elevasis/core 0.1.0 → 0.2.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 (35) hide show
  1. package/dist/index.js +195 -3
  2. package/dist/organization-model/index.js +195 -3
  3. package/package.json +1 -1
  4. package/src/README.md +24 -17
  5. package/src/__tests__/template-foundations-compatibility.test.ts +95 -14
  6. package/src/auth/multi-tenancy/types.ts +2 -1
  7. package/src/execution/engine/__tests__/fixtures/test-agents.ts +4 -4
  8. package/src/execution/engine/index.ts +5 -19
  9. package/src/execution/engine/tools/platform/index.ts +9 -33
  10. package/src/execution/engine/tools/registry.ts +109 -2
  11. package/src/execution/engine/tools/tool-maps.ts +88 -0
  12. package/src/organization-model/README.md +19 -4
  13. package/src/organization-model/__tests__/graph.test.ts +612 -0
  14. package/src/organization-model/__tests__/resolve.test.ts +208 -0
  15. package/src/organization-model/defaults.ts +1 -1
  16. package/src/organization-model/organization-graph.mdx +262 -0
  17. package/src/organization-model/organization-model.mdx +257 -0
  18. package/src/organization-model/resolve.ts +26 -2
  19. package/src/organization-model/schema.ts +203 -1
  20. package/src/platform/constants/versions.ts +1 -1
  21. package/src/platform/registry/__tests__/resource-registry.integration.test.ts +24 -0
  22. package/src/platform/registry/__tests__/resource-registry.test.ts +63 -0
  23. package/src/platform/registry/resource-registry.ts +98 -10
  24. package/src/projects/api-schemas.ts +2 -1
  25. package/src/reference/_generated/contracts.md +1044 -0
  26. package/src/reference/glossary.md +88 -0
  27. package/src/server.ts +2 -3
  28. package/src/execution/engine/tools/platform/resource-invocation/__tests__/edge-cases.test.ts +0 -507
  29. package/src/execution/engine/tools/platform/resource-invocation/__tests__/resource-invocation-service.test.ts +0 -500
  30. package/src/execution/engine/tools/platform/resource-invocation/__tests__/tool.test.ts +0 -555
  31. package/src/execution/engine/tools/platform/resource-invocation/dynamic-tool.ts +0 -94
  32. package/src/execution/engine/tools/platform/resource-invocation/index.ts +0 -14
  33. package/src/execution/engine/tools/platform/resource-invocation/resource-invocation-service.ts +0 -147
  34. package/src/execution/engine/tools/platform/resource-invocation/tool.ts +0 -115
  35. package/src/execution/engine/tools/platform/resource-invocation/types.ts +0 -31
@@ -1,147 +0,0 @@
1
- import type { ExecutionContext } from '../../../base/types'
2
- import type { ResourceRegistry } from '../../../../../platform/registry/resource-registry'
3
- import type { ResourceExecutionRequest, ResourceInvocationResult } from './types'
4
-
5
- /**
6
- * ResourceInvocationService
7
- * Handles execution of resources (agents and workflows) from agent context
8
- *
9
- * Responsibilities:
10
- * - Validate resource exists and is accessible
11
- * - Validate input against resource contract
12
- * - Execute resource with parent execution context
13
- * - Return typed invocation result
14
- *
15
- * Multi-tenancy: All resource lookups scoped by organizationName (passed as parameter)
16
- */
17
- export class ResourceInvocationService {
18
- constructor(
19
- private registry: ResourceRegistry,
20
- private executeResource: (
21
- request: ResourceExecutionRequest,
22
- parentContext: ExecutionContext
23
- ) => Promise<ResourceInvocationResult>
24
- ) {}
25
-
26
- /**
27
- * Execute resource synchronously (waits for completion)
28
- *
29
- * @param resourceId - ID of resource to execute (agent or workflow)
30
- * @param input - Input payload for resource
31
- * @param organizationName - Organization name for resource lookup (from execution request)
32
- * @param parentContext - Parent execution context (provides organization ID and tracking)
33
- * @returns Invocation result with success, output, executionId, and resourceType
34
- *
35
- * @example
36
- * const result = await service.executeSync(
37
- * 'lead-validation-workflow',
38
- * { email: 'test@test.com', name: 'John Doe' },
39
- * 'acme-corp',
40
- * executionContext
41
- * )
42
- *
43
- * if (result.success) {
44
- * console.log('Validation result:', result.output)
45
- * } else {
46
- * console.error('Validation failed:', result.error)
47
- * }
48
- */
49
- async executeSync(
50
- resourceId: string,
51
- input: unknown,
52
- organizationName: string,
53
- parentContext: ExecutionContext
54
- ): Promise<ResourceInvocationResult> {
55
- // 1. Validate resource exists (scoped to organization)
56
- const definition = this.registry.getResourceDefinition(
57
- organizationName,
58
- resourceId
59
- )
60
-
61
- if (!definition) {
62
- return {
63
- success: false,
64
- output: null,
65
- executionId: '',
66
- resourceType: 'workflow',
67
- error: `Resource not found: ${resourceId}`
68
- }
69
- }
70
-
71
- // Determine resource type from definition
72
- const resourceType = definition.config.type as 'agent' | 'workflow'
73
-
74
- // 2. Enforce relationship declaration
75
- const relationships = this.registry.getRelationships(organizationName)
76
- if (relationships) {
77
- const callerRel = relationships[parentContext.resourceId]
78
- const declaredTargets = [
79
- ...(callerRel?.triggers?.agents ?? []),
80
- ...(callerRel?.triggers?.workflows ?? [])
81
- ]
82
- if (!declaredTargets.includes(resourceId)) {
83
- throw new Error(
84
- `Undeclared resource invocation: "${parentContext.resourceId}" -> "${resourceId}". ` +
85
- `Add to relationships: { '${parentContext.resourceId}': { triggers: { ${resourceType}s: ['${resourceId}'] } } }`
86
- )
87
- }
88
- }
89
-
90
- // 3. Validate input against resource contract
91
- const validationResult = definition.contract.inputSchema.safeParse(input)
92
- if (!validationResult.success) {
93
- return {
94
- success: false,
95
- output: null,
96
- executionId: '',
97
- resourceType,
98
- error: `Input validation failed: ${validationResult.error.message}`
99
- }
100
- }
101
-
102
- // 4. Execute resource with parent context
103
- const result = await this.executeResource(
104
- {
105
- resourceId,
106
- input: validationResult.data,
107
- organizationId: parentContext.organizationId,
108
- organizationName
109
- },
110
- parentContext
111
- )
112
-
113
- return {
114
- ...result,
115
- resourceType
116
- }
117
- }
118
- }
119
-
120
- /**
121
- * Factory function for creating ResourceInvocationService
122
- * Validates dependencies at construction time (fail-fast)
123
- *
124
- * @param registry - ResourceRegistry for resource lookup
125
- * @param executeResource - Callback to execute resource (delegates to coordinator)
126
- * @returns ResourceInvocationService instance
127
- * @throws Error if dependencies are missing
128
- *
129
- * @example
130
- * const service = createResourceInvocationService(
131
- * resourceRegistry,
132
- * async (request, parentContext) => {
133
- * return coordinator.executeNested(request, parentContext)
134
- * }
135
- * )
136
- */
137
- export function createResourceInvocationService(
138
- registry: ResourceRegistry,
139
- executeResource: (
140
- request: ResourceExecutionRequest,
141
- parentContext: ExecutionContext
142
- ) => Promise<ResourceInvocationResult>
143
- ): ResourceInvocationService {
144
- if (!registry) throw new Error('ResourceRegistry required')
145
- if (!executeResource) throw new Error('executeResource callback required')
146
- return new ResourceInvocationService(registry, executeResource)
147
- }
@@ -1,115 +0,0 @@
1
- /**
2
- * Resource Invocation Tool Factory
3
- *
4
- * Creates tools that invoke other resources (agents or workflows) as first-class tools.
5
- * Schemas are derived directly from the resource's contract, eliminating duplication.
6
- *
7
- * @module resource-invocation/tool
8
- */
9
-
10
- import { z } from 'zod'
11
- import { serviceUnavailable, apiError } from '../../types'
12
- import type { Tool, ToolExecutionOptions } from '../../types'
13
- import type { AgentDefinition } from '../../../agent/core/types'
14
- import type { WorkflowDefinition } from '../../../workflow/types'
15
- import { getToolServices } from '../../registry'
16
- import { DEFAULT_RESOURCE_INVOCATION_TIMEOUT } from '../../../../../platform/constants/timeouts'
17
-
18
- /**
19
- * Resource definition type (Agent or Workflow)
20
- * Both types have config and contract fields needed for tool creation
21
- */
22
- export type ResourceDefinition = AgentDefinition | WorkflowDefinition
23
-
24
- /**
25
- * Configuration for creating a resource invocation tool
26
- *
27
- * The tool factory derives schemas from resource.contract automatically,
28
- * ensuring type safety and eliminating manual schema duplication.
29
- */
30
- export interface ResourceInvocationToolConfig {
31
- /** Tool name for LLM selection (e.g., 'invoke_lead_validation') */
32
- name: string
33
-
34
- /** Description of what the tool does (shown to LLM) */
35
- description: string
36
-
37
- /** Target resource definition - schemas derived automatically from contract */
38
- resource: ResourceDefinition
39
- }
40
-
41
- /**
42
- * Create a tool that invokes another resource (agent or workflow)
43
- *
44
- * Schemas are derived directly from the resource's contract:
45
- * - inputSchema: From resource.contract.inputSchema
46
- * - outputSchema: From resource.contract.outputSchema (or z.null() if not defined)
47
- *
48
- * This ensures type safety and eliminates schema duplication.
49
- *
50
- * @param config - Tool configuration with name, description, and target resource
51
- * @returns Tool that invokes the resource via ResourceInvocationService
52
- *
53
- * @example
54
- * // Invoke workflow with derived schemas
55
- * const leadValidationTool = createResourceInvocationTool({
56
- * name: 'invoke_lead_validation',
57
- * description: 'Validate scraped lead data for completeness and format',
58
- * resource: leadValidationWorkflow // Schemas derived from contract
59
- * })
60
- *
61
- * @example
62
- * // Invoke agent with derived schemas
63
- * const companyResearchTool = createResourceInvocationTool({
64
- * name: 'invoke_company_research',
65
- * description: 'Research company details using AI reasoning',
66
- * resource: companyResearchAgent // Schemas derived from contract
67
- * })
68
- */
69
- export function createResourceInvocationTool(
70
- config: ResourceInvocationToolConfig
71
- ): Tool {
72
- const { resource } = config
73
-
74
- // Derive schemas from resource contract - no manual duplication
75
- const inputSchema = resource.contract.inputSchema
76
- const outputSchema = resource.contract.outputSchema ?? z.null()
77
-
78
- return {
79
- name: config.name,
80
- description: config.description,
81
- inputSchema,
82
- outputSchema,
83
- timeout: DEFAULT_RESOURCE_INVOCATION_TIMEOUT,
84
-
85
- async execute(options: ToolExecutionOptions): Promise<unknown> {
86
- // Get resource invocation service from global registry
87
- const { resourceInvocationService } = getToolServices()
88
-
89
- if (!resourceInvocationService) {
90
- throw serviceUnavailable('ResourceInvocationService')
91
- }
92
-
93
- if (!options.executionContext) {
94
- throw serviceUnavailable('ExecutionContext', { tool: config.name, reason: 'organizationId required for multi-tenant isolation' })
95
- }
96
-
97
- // Execute resource and get result
98
- // organizationName is guaranteed to be present in ExecutionContext (extends ExecutionMetadata)
99
- const result = await resourceInvocationService.executeSync(
100
- resource.config.resourceId,
101
- options.input,
102
- options.executionContext.organizationName,
103
- options.executionContext
104
- )
105
-
106
- // Throw error if execution failed
107
- if (!result.success) {
108
- throw apiError(result.error || `${result.resourceType} execution failed`)
109
- }
110
-
111
- // Return output (null for fire-and-forget resources without outputSchema)
112
- return result.output
113
- }
114
- }
115
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * Resource execution request parameters
3
- * Contains resource identification and input payload for execution
4
- */
5
- export interface ResourceExecutionRequest {
6
- /** Resource ID to execute (agent or workflow) */
7
- resourceId: string
8
- /** Input payload for the resource (validated against resource contract) */
9
- input: unknown
10
- /** Organization ID for multi-tenancy isolation */
11
- organizationId: string
12
- /** Organization name for resource lookup */
13
- organizationName: string
14
- }
15
-
16
- /**
17
- * Resource invocation result
18
- * Returned from executeSync() after resource execution completes
19
- */
20
- export interface ResourceInvocationResult {
21
- /** Whether execution succeeded */
22
- success: boolean
23
- /** Output from resource (TOutput from contract, or null if no outputSchema) */
24
- output: unknown
25
- /** Execution ID for tracking and observability */
26
- executionId: string
27
- /** Type of resource executed (agent or workflow) */
28
- resourceType: 'agent' | 'workflow'
29
- /** Error message if execution failed */
30
- error?: string
31
- }