File size: 4,984 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * Agent-scope domain service contracts. These mirror the signatures of the
 * engine's domain Services (prompt / skill / loop / permissionMode / command /
 * contextMemory / tokenCounting / shellCommand / profile / usage / plan /
 * task) that the agent facade calls directly; payload and result schemas are
 * shared in `agent/schemas.ts` (they mirror the same wire shapes).
 */

import { z } from 'zod';

import { maybe, noResult } from '../helpers.js';
import type { ServiceContract } from '../types.js';
import {
  activateSkillPayloadSchema,
  agentCommandInfoSchema,
  agentTaskInfoSchema,
  permissionModeSchema,
  planDataSchema,
  promptLaunchResultSchema,
  promptPayloadSchema,
  promptWithSkillsPayloadSchema,
  promptWithSkillsResultSchema,
  runShellCommandPayloadSchema,
  runtimeBindingSchema,
  setModelResultSchema,
  shellCommandResultSchema,
  steerPayloadSchema,
  usageStatusSchema,
} from './schemas.js';

export const agentPromptContract = {
  submit: {
    input: z.tuple([promptPayloadSchema]),
    output: maybe(promptLaunchResultSchema),
  },
  submitSteer: {
    input: z.tuple([steerPayloadSchema]),
    output: maybe(promptLaunchResultSchema),
  },
} satisfies ServiceContract;

export const agentSkillContract = {
  activate: { input: z.tuple([activateSkillPayloadSchema]), output: promptLaunchResultSchema },
  promptWithSkills: {
    input: z.tuple([promptWithSkillsPayloadSchema]),
    output: promptWithSkillsResultSchema,
  },
} satisfies ServiceContract;

export const agentLoopContract = {
  cancelFromUser: { input: z.tuple([z.number().optional()]), output: noResult },
} satisfies ServiceContract;

export const agentPermissionModeContract = {
  setModeAndBroadcast: { input: z.tuple([permissionModeSchema]), output: noResult },
} satisfies ServiceContract;

export const agentCommandContract = {
  list: { input: z.tuple([]), output: z.array(agentCommandInfoSchema) },
  run: { input: z.tuple([z.string(), z.string().optional()]), output: noResult },
} satisfies ServiceContract;

export const agentRuntimeBindingContract = {
  get: { input: z.tuple([]), output: runtimeBindingSchema },
  set: { input: z.tuple([runtimeBindingSchema]), output: runtimeBindingSchema },
  switch: { input: z.tuple([z.string()]), output: runtimeBindingSchema },
} satisfies ServiceContract;

/** `history` items are full `ContextMessage`s, mirrored as `unknown`. */
export const agentContextMemoryContract = {
  get: { input: z.tuple([]), output: z.array(z.unknown()) },
} satisfies ServiceContract;

export const agentTokenCountingContract = {
  statusSize: { input: z.tuple([]), output: z.number() },
} satisfies ServiceContract;

export const agentShellCommandContract = {
  run: {
    input: z.tuple([runShellCommandPayloadSchema]),
    output: shellCommandResultSchema,
  },
  cancel: { input: z.tuple([z.string()]), output: noResult },
} satisfies ServiceContract;

export const agentProfileContract = {
  getModel: { input: z.tuple([]), output: z.string() },
  setModel: { input: z.tuple([z.string()]), output: setModelResultSchema },
  setThinking: { input: z.tuple([z.string()]), output: noResult },
  getEffectiveThinkingLevel: { input: z.tuple([]), output: z.string() },
} satisfies ServiceContract;

export const agentUsageContract = {
  status: { input: z.tuple([]), output: usageStatusSchema },
} satisfies ServiceContract;

export const agentPlanContract = {
  status: { input: z.tuple([]), output: planDataSchema },
  enter: { input: z.tuple([]), output: noResult },
  clear: { input: z.tuple([]), output: noResult },
  cancel: { input: z.tuple([z.string().optional()]), output: noResult },
} satisfies ServiceContract;

/** `McpServerEntry` from the engine's `mcpCore/connection-manager`. */
export const mcpServerEntrySchema = z.object({
  name: z.string(),
  transport: z.enum(['stdio', 'http', 'sse']),
  status: z.enum(['pending', 'connected', 'failed', 'disabled', 'needs-auth', 'removed']),
  toolCount: z.number(),
  error: z.string().optional(),
});

export const agentMcpContract = {
  list: { input: z.tuple([]), output: z.array(mcpServerEntrySchema) },
} satisfies ServiceContract;

/** `FullCompactionInput` from the engine's `agent/fullCompaction`. */
export const fullCompactionInputSchema = z.object({
  source: z.enum(['manual', 'auto']),
  instruction: z.string().optional(),
});

export const agentFullCompactionContract = {
  begin: { input: z.tuple([fullCompactionInputSchema]), output: z.boolean() },
} satisfies ServiceContract;

export const agentTaskContract = {
  list: {
    input: z.tuple([z.boolean().optional(), z.number().optional()]),
    output: z.array(agentTaskInfoSchema),
  },
  stopByUser: { input: z.tuple([z.string()]), output: maybe(agentTaskInfoSchema) },
  stop: {
    input: z.tuple([z.string(), z.string().optional()]),
    output: maybe(agentTaskInfoSchema),
  },
  readOutput: {
    input: z.tuple([z.string(), z.number().optional()]),
    output: z.string(),
  },
} satisfies ServiceContract;