m365-agent-cli 1.2.0

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 (92) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +916 -0
  3. package/package.json +50 -0
  4. package/src/cli.ts +100 -0
  5. package/src/commands/auto-reply.ts +182 -0
  6. package/src/commands/calendar.ts +576 -0
  7. package/src/commands/counter.ts +87 -0
  8. package/src/commands/create-event.ts +544 -0
  9. package/src/commands/delegates.ts +286 -0
  10. package/src/commands/delete-event.ts +321 -0
  11. package/src/commands/drafts.ts +502 -0
  12. package/src/commands/files.ts +532 -0
  13. package/src/commands/find.ts +195 -0
  14. package/src/commands/findtime.ts +270 -0
  15. package/src/commands/folders.ts +177 -0
  16. package/src/commands/forward-event.ts +49 -0
  17. package/src/commands/graph-calendar.ts +217 -0
  18. package/src/commands/login.ts +195 -0
  19. package/src/commands/mail.ts +950 -0
  20. package/src/commands/oof.ts +263 -0
  21. package/src/commands/outlook-categories.ts +173 -0
  22. package/src/commands/outlook-graph.ts +880 -0
  23. package/src/commands/planner.ts +1678 -0
  24. package/src/commands/respond.ts +291 -0
  25. package/src/commands/rooms.ts +210 -0
  26. package/src/commands/rules.ts +511 -0
  27. package/src/commands/schedule.ts +109 -0
  28. package/src/commands/send.ts +204 -0
  29. package/src/commands/serve.ts +14 -0
  30. package/src/commands/sharepoint.ts +179 -0
  31. package/src/commands/site-pages.ts +163 -0
  32. package/src/commands/subscribe.ts +103 -0
  33. package/src/commands/subscriptions.ts +29 -0
  34. package/src/commands/suggest.ts +155 -0
  35. package/src/commands/todo.ts +2092 -0
  36. package/src/commands/update-event.ts +608 -0
  37. package/src/commands/update.ts +88 -0
  38. package/src/commands/verify-token.ts +62 -0
  39. package/src/commands/whoami.ts +74 -0
  40. package/src/index.ts +190 -0
  41. package/src/lib/atomic-write.ts +20 -0
  42. package/src/lib/attach-link-spec.test.ts +24 -0
  43. package/src/lib/attach-link-spec.ts +70 -0
  44. package/src/lib/attachments.ts +79 -0
  45. package/src/lib/auth.ts +192 -0
  46. package/src/lib/calendar-range.test.ts +41 -0
  47. package/src/lib/calendar-range.ts +103 -0
  48. package/src/lib/dates.test.ts +74 -0
  49. package/src/lib/dates.ts +137 -0
  50. package/src/lib/delegate-client.test.ts +74 -0
  51. package/src/lib/delegate-client.ts +322 -0
  52. package/src/lib/ews-client.ts +3418 -0
  53. package/src/lib/git-commit.ts +4 -0
  54. package/src/lib/glitchtip-eligibility.ts +220 -0
  55. package/src/lib/glitchtip.ts +253 -0
  56. package/src/lib/global-env.ts +3 -0
  57. package/src/lib/graph-auth.ts +223 -0
  58. package/src/lib/graph-calendar-client.test.ts +118 -0
  59. package/src/lib/graph-calendar-client.ts +112 -0
  60. package/src/lib/graph-client.test.ts +107 -0
  61. package/src/lib/graph-client.ts +1058 -0
  62. package/src/lib/graph-constants.ts +12 -0
  63. package/src/lib/graph-directory.ts +116 -0
  64. package/src/lib/graph-event.ts +134 -0
  65. package/src/lib/graph-schedule.ts +173 -0
  66. package/src/lib/graph-subscriptions.ts +94 -0
  67. package/src/lib/graph-user-path.ts +13 -0
  68. package/src/lib/jwt-utils.ts +34 -0
  69. package/src/lib/markdown.test.ts +21 -0
  70. package/src/lib/markdown.ts +174 -0
  71. package/src/lib/mime-type.ts +106 -0
  72. package/src/lib/oof-client.test.ts +59 -0
  73. package/src/lib/oof-client.ts +122 -0
  74. package/src/lib/outlook-graph-client.test.ts +146 -0
  75. package/src/lib/outlook-graph-client.ts +649 -0
  76. package/src/lib/outlook-master-categories.ts +145 -0
  77. package/src/lib/package-info.ts +59 -0
  78. package/src/lib/places-client.ts +144 -0
  79. package/src/lib/planner-client.ts +1226 -0
  80. package/src/lib/rules-client.ts +178 -0
  81. package/src/lib/sharepoint-client.ts +101 -0
  82. package/src/lib/site-pages-client.ts +73 -0
  83. package/src/lib/todo-client.test.ts +298 -0
  84. package/src/lib/todo-client.ts +1309 -0
  85. package/src/lib/url-validation.ts +40 -0
  86. package/src/lib/utils.ts +45 -0
  87. package/src/lib/webhook-server.ts +51 -0
  88. package/src/test/auth.test.ts +104 -0
  89. package/src/test/cli.integration.test.ts +1083 -0
  90. package/src/test/ews-client.test.ts +268 -0
  91. package/src/test/mocks/index.ts +375 -0
  92. package/src/test/mocks/responses.ts +861 -0
@@ -0,0 +1,145 @@
1
+ import { callGraph, GraphApiError, type GraphResponse, graphError, graphResult } from './graph-client.js';
2
+ import { graphUserPath } from './graph-user-path.js';
3
+
4
+ /** Preset color name from Graph (`preset0`..`preset24`). */
5
+ export interface OutlookMasterCategory {
6
+ id: string;
7
+ displayName: string;
8
+ color: string;
9
+ }
10
+
11
+ /** Valid Microsoft Graph outlookCategory color constants (25 presets). */
12
+ export const OUTLOOK_CATEGORY_COLOR_PRESETS = [
13
+ 'preset0',
14
+ 'preset1',
15
+ 'preset2',
16
+ 'preset3',
17
+ 'preset4',
18
+ 'preset5',
19
+ 'preset6',
20
+ 'preset7',
21
+ 'preset8',
22
+ 'preset9',
23
+ 'preset10',
24
+ 'preset11',
25
+ 'preset12',
26
+ 'preset13',
27
+ 'preset14',
28
+ 'preset15',
29
+ 'preset16',
30
+ 'preset17',
31
+ 'preset18',
32
+ 'preset19',
33
+ 'preset20',
34
+ 'preset21',
35
+ 'preset22',
36
+ 'preset23',
37
+ 'preset24'
38
+ ] as const;
39
+
40
+ export type OutlookCategoryColorPreset = (typeof OUTLOOK_CATEGORY_COLOR_PRESETS)[number];
41
+
42
+ export function isValidOutlookCategoryColor(s: string): s is OutlookCategoryColorPreset {
43
+ return OUTLOOK_CATEGORY_COLOR_PRESETS.includes(s as OutlookCategoryColorPreset);
44
+ }
45
+
46
+ export async function listOutlookMasterCategories(
47
+ token: string,
48
+ user?: string
49
+ ): Promise<GraphResponse<OutlookMasterCategory[]>> {
50
+ const path = graphUserPath(user, 'outlook/masterCategories');
51
+ let result: GraphResponse<{ value: OutlookMasterCategory[] }>;
52
+ try {
53
+ result = await callGraph<{ value: OutlookMasterCategory[] }>(token, path);
54
+ } catch (err) {
55
+ if (err instanceof GraphApiError) {
56
+ return graphError(err.message, err.code, err.status);
57
+ }
58
+ return graphError(err instanceof Error ? err.message : 'Failed to list master categories');
59
+ }
60
+ if (!result.ok || !result.data) {
61
+ return graphError(
62
+ result.error?.message || 'Failed to list master categories',
63
+ result.error?.code,
64
+ result.error?.status
65
+ );
66
+ }
67
+ return graphResult(result.data.value);
68
+ }
69
+
70
+ export async function createOutlookMasterCategory(
71
+ token: string,
72
+ displayName: string,
73
+ color: string,
74
+ user?: string
75
+ ): Promise<GraphResponse<OutlookMasterCategory>> {
76
+ const path = graphUserPath(user, 'outlook/masterCategories');
77
+ try {
78
+ const result = await callGraph<OutlookMasterCategory>(token, path, {
79
+ method: 'POST',
80
+ body: JSON.stringify({ displayName: displayName.trim(), color })
81
+ });
82
+ if (!result.ok || !result.data) {
83
+ return graphError(
84
+ result.error?.message || 'Failed to create master category',
85
+ result.error?.code,
86
+ result.error?.status
87
+ );
88
+ }
89
+ return graphResult(result.data);
90
+ } catch (err) {
91
+ if (err instanceof GraphApiError) return graphError(err.message, err.code, err.status);
92
+ return graphError(err instanceof Error ? err.message : 'Failed to create master category');
93
+ }
94
+ }
95
+
96
+ export async function updateOutlookMasterCategory(
97
+ token: string,
98
+ categoryId: string,
99
+ updates: { displayName?: string; color?: string },
100
+ user?: string
101
+ ): Promise<GraphResponse<OutlookMasterCategory>> {
102
+ const path = `${graphUserPath(user, 'outlook/masterCategories')}/${encodeURIComponent(categoryId)}`;
103
+ try {
104
+ const body: Record<string, string> = {};
105
+ if (updates.displayName !== undefined) body.displayName = updates.displayName.trim();
106
+ if (updates.color !== undefined) body.color = updates.color;
107
+ const result = await callGraph<OutlookMasterCategory>(token, path, {
108
+ method: 'PATCH',
109
+ body: JSON.stringify(body)
110
+ });
111
+ if (!result.ok || !result.data) {
112
+ return graphError(
113
+ result.error?.message || 'Failed to update master category',
114
+ result.error?.code,
115
+ result.error?.status
116
+ );
117
+ }
118
+ return graphResult(result.data);
119
+ } catch (err) {
120
+ if (err instanceof GraphApiError) return graphError(err.message, err.code, err.status);
121
+ return graphError(err instanceof Error ? err.message : 'Failed to update master category');
122
+ }
123
+ }
124
+
125
+ export async function deleteOutlookMasterCategory(
126
+ token: string,
127
+ categoryId: string,
128
+ user?: string
129
+ ): Promise<GraphResponse<void>> {
130
+ const path = `${graphUserPath(user, 'outlook/masterCategories')}/${encodeURIComponent(categoryId)}`;
131
+ try {
132
+ const result = await callGraph<void>(token, path, { method: 'DELETE' });
133
+ if (!result.ok) {
134
+ return graphError(
135
+ result.error?.message || 'Failed to delete master category',
136
+ result.error?.code,
137
+ result.error?.status
138
+ );
139
+ }
140
+ return graphResult(undefined as undefined);
141
+ } catch (err) {
142
+ if (err instanceof GraphApiError) return graphError(err.message, err.code, err.status);
143
+ return graphError(err instanceof Error ? err.message : 'Failed to delete master category');
144
+ }
145
+ }
@@ -0,0 +1,59 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const PKG_NAME = 'm365-agent-cli';
6
+
7
+ function isOurPackageJson(path: string): boolean {
8
+ try {
9
+ const raw = readFileSync(path, 'utf8');
10
+ const j = JSON.parse(raw) as { name?: string };
11
+ return j.name === PKG_NAME;
12
+ } catch {
13
+ return false;
14
+ }
15
+ }
16
+
17
+ function walkUpForOurPackageJson(startDir: string): string | null {
18
+ let dir = startDir;
19
+ for (let i = 0; i < 14; i++) {
20
+ const candidate = join(dir, 'package.json');
21
+ if (existsSync(candidate) && isOurPackageJson(candidate)) {
22
+ return candidate;
23
+ }
24
+ const parent = dirname(dir);
25
+ if (parent === dir) break;
26
+ dir = parent;
27
+ }
28
+ return null;
29
+ }
30
+
31
+ /**
32
+ * Resolves package.json for this package (installed layout, repo checkout, or tests).
33
+ */
34
+ export function getPackageJsonPath(): string {
35
+ const libDir = dirname(fileURLToPath(import.meta.url));
36
+ const fromAdjacent = join(libDir, '../../package.json');
37
+ if (existsSync(fromAdjacent) && isOurPackageJson(fromAdjacent)) {
38
+ return fromAdjacent;
39
+ }
40
+ const fromModuleWalk = walkUpForOurPackageJson(libDir);
41
+ if (fromModuleWalk) {
42
+ return fromModuleWalk;
43
+ }
44
+ const fromCwdWalk = walkUpForOurPackageJson(process.cwd());
45
+ if (fromCwdWalk) {
46
+ return fromCwdWalk;
47
+ }
48
+ return fromAdjacent;
49
+ }
50
+
51
+ /** Uses sync fs so tests that mock `node:fs/promises` (e.g. auth) do not break version reads. */
52
+ export async function getPackageVersion(): Promise<string> {
53
+ return getPackageVersionSync();
54
+ }
55
+
56
+ export function getPackageVersionSync(): string {
57
+ const raw = readFileSync(getPackageJsonPath(), 'utf8');
58
+ return (JSON.parse(raw) as { version?: string }).version?.trim() ?? '0.0.0';
59
+ }
@@ -0,0 +1,144 @@
1
+ import { resolveGraphAuth } from './graph-auth.js';
2
+ import { callGraph, fetchAllPages, graphError, graphResult } from './graph-client.js';
3
+
4
+ export interface Place {
5
+ id?: string;
6
+ displayName: string;
7
+ emailAddress?: string;
8
+ address?: {
9
+ street?: string;
10
+ city?: string;
11
+ state?: string;
12
+ countryOrRegion?: string;
13
+ postalCode?: string;
14
+ fullAddress?: string;
15
+ };
16
+ geoCoordinates?: {
17
+ latitude?: number;
18
+ longitude?: number;
19
+ };
20
+ capacity?: number;
21
+ bookingType?: 'standard' | 'reserved';
22
+ tags?: string[];
23
+ building?: string;
24
+ floorNumber?: string;
25
+ isManaged?: boolean;
26
+ isBookable?: boolean;
27
+ phone?: string;
28
+ }
29
+
30
+ export interface RoomList {
31
+ id: string;
32
+ displayName: string;
33
+ emailAddress?: string;
34
+ }
35
+
36
+ async function withAuth<T>(
37
+ fn: (
38
+ token: string
39
+ ) => Promise<{ ok: boolean; data?: T; error?: { message: string; code?: string; status?: number } }>,
40
+ options?: { token?: string; identity?: string }
41
+ ): Promise<{ ok: boolean; data?: T; error?: { message: string; code?: string; status?: number } }> {
42
+ const auth = await resolveGraphAuth(options);
43
+ if (!auth.success || !auth.token) {
44
+ return graphError(auth.error || 'Authentication failed');
45
+ }
46
+ return fn(auth.token);
47
+ }
48
+
49
+ export async function listPlaceRoomLists(options?: { token?: string; identity?: string }): Promise<{
50
+ ok: boolean;
51
+ data?: RoomList[];
52
+ error?: { message: string; code?: string; status?: number };
53
+ }> {
54
+ return withAuth<RoomList[]>(async (token) => {
55
+ return fetchAllPages<RoomList>(token, '/places/microsoft.graph.roomList', 'Failed to list room lists');
56
+ }, options);
57
+ }
58
+
59
+ export async function listRoomsInRoomList(
60
+ roomListEmail: string,
61
+ options?: { token?: string; identity?: string }
62
+ ): Promise<{
63
+ ok: boolean;
64
+ data?: Place[];
65
+ error?: { message: string; code?: string; status?: number };
66
+ }> {
67
+ return withAuth<Place[]>(async (token) => {
68
+ return fetchAllPages<Place>(
69
+ token,
70
+ `/places/${encodeURIComponent(roomListEmail)}/microsoft.graph.roomlist/rooms`,
71
+ 'Failed to list rooms'
72
+ );
73
+ }, options);
74
+ }
75
+
76
+ export interface RoomFilters {
77
+ building?: string;
78
+ capacityMin?: number;
79
+ equipment?: string[];
80
+ }
81
+
82
+ export async function findRooms(
83
+ filters?: RoomFilters,
84
+ options?: { token?: string; identity?: string }
85
+ ): Promise<{
86
+ ok: boolean;
87
+ data?: Place[];
88
+ error?: { message: string; code?: string; status?: number };
89
+ }> {
90
+ return withAuth<Place[]>(async (token) => {
91
+ const result = await fetchAllPages<Place>(token, '/places/microsoft.graph.room', 'Failed to find rooms');
92
+ if (!result.ok || !result.data) {
93
+ return result;
94
+ }
95
+
96
+ let rooms = result.data;
97
+
98
+ if (filters?.building) {
99
+ const buildingLower = filters.building.toLowerCase();
100
+ rooms = rooms.filter((r) => r.building?.toLowerCase().includes(buildingLower));
101
+ }
102
+
103
+ if (filters?.capacityMin !== undefined) {
104
+ rooms = rooms.filter((r) => r.capacity !== undefined && r.capacity >= filters.capacityMin!);
105
+ }
106
+
107
+ if (filters?.equipment && filters.equipment.length > 0) {
108
+ const equipLower = filters.equipment.map((e) => e.toLowerCase());
109
+ rooms = rooms.filter((r) => {
110
+ if (!r.tags || r.tags.length === 0) return false;
111
+ const tagsLower = r.tags.map((t) => t.toLowerCase());
112
+ return equipLower.every((e) => tagsLower.some((t) => t.includes(e)));
113
+ });
114
+ }
115
+
116
+ return graphResult(rooms);
117
+ }, options);
118
+ }
119
+
120
+ export async function isRoomFree(
121
+ token: string,
122
+ roomEmail: string,
123
+ startISO: string,
124
+ endISO: string
125
+ ): Promise<boolean | null> {
126
+ let result: any;
127
+ try {
128
+ result = await callGraph<{ value: Array<{ showAs?: string }> }>(
129
+ token,
130
+ `/users/${encodeURIComponent(roomEmail)}/calendar/calendarView?startDateTime=${encodeURIComponent(
131
+ startISO
132
+ )}&endDateTime=${encodeURIComponent(endISO)}`
133
+ );
134
+ } catch (_err) {
135
+ return null;
136
+ }
137
+
138
+ if (!result.ok || !result.data) {
139
+ return null;
140
+ }
141
+
142
+ const busyEvents = (result.data.value || []).filter((event: any) => event.showAs !== 'free');
143
+ return busyEvents.length === 0;
144
+ }