@midsummer-ai/sdk 0.1.0-staging.6023 → 0.1.0-staging.6030
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.
- package/here-BO-wQDjz.mjs +1 -1
- package/package.json +8 -1
- package/repository.d.mts +146 -0
- package/repository.mjs +125 -0
package/here-BO-wQDjz.mjs
CHANGED
|
@@ -21,7 +21,7 @@ function processEnv() {
|
|
|
21
21
|
* This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
|
|
22
22
|
* release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
|
|
23
23
|
*/
|
|
24
|
-
const SDK_VERSION = "0.1.0-staging.
|
|
24
|
+
const SDK_VERSION = "0.1.0-staging.6030";
|
|
25
25
|
/**
|
|
26
26
|
* The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
|
|
27
27
|
* <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-staging.
|
|
3
|
+
"version": "0.1.0-staging.6030",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
|
|
@@ -207,6 +207,10 @@
|
|
|
207
207
|
"types": "./apps.d.mts",
|
|
208
208
|
"import": "./apps.mjs"
|
|
209
209
|
},
|
|
210
|
+
"./repository": {
|
|
211
|
+
"types": "./repository.d.mts",
|
|
212
|
+
"import": "./repository.mjs"
|
|
213
|
+
},
|
|
210
214
|
"./email/client": {
|
|
211
215
|
"types": "./email/client.d.mts",
|
|
212
216
|
"import": "./email/client.mjs"
|
|
@@ -449,6 +453,9 @@
|
|
|
449
453
|
"apps": [
|
|
450
454
|
"./apps.d.mts"
|
|
451
455
|
],
|
|
456
|
+
"repository": [
|
|
457
|
+
"./repository.d.mts"
|
|
458
|
+
],
|
|
452
459
|
"email/client": [
|
|
453
460
|
"./email/client.d.mts"
|
|
454
461
|
],
|
package/repository.d.mts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
//#region src/repository.d.ts
|
|
2
|
+
type RepositoryErrorCode = "invalid" | "forbidden" | "not-found" | "conflict" | "unavailable";
|
|
3
|
+
declare class RepositoryError extends Error {
|
|
4
|
+
readonly code: RepositoryErrorCode;
|
|
5
|
+
constructor(code: RepositoryErrorCode, message: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The Action each call is, by its method and its path under `/v1`, as `@midsummer/actions` declares it; a test keeps the
|
|
9
|
+
* two the same, so this door never drifts from the others.
|
|
10
|
+
*/
|
|
11
|
+
declare const REPOSITORY_ACTIONS: {
|
|
12
|
+
readonly list: {
|
|
13
|
+
readonly action: "repo.file.list";
|
|
14
|
+
readonly method: "GET";
|
|
15
|
+
readonly path: "/repo/:team/:repository/branches/:branch/files";
|
|
16
|
+
};
|
|
17
|
+
readonly read: {
|
|
18
|
+
readonly action: "repo.file.show";
|
|
19
|
+
readonly method: "GET";
|
|
20
|
+
readonly path: "/repo/:team/:repository/branches/:branch/file";
|
|
21
|
+
};
|
|
22
|
+
readonly search: {
|
|
23
|
+
readonly action: "repo.search";
|
|
24
|
+
readonly method: "GET";
|
|
25
|
+
readonly path: "/orbit/:team/:repository/branches/:branch/search";
|
|
26
|
+
};
|
|
27
|
+
readonly save: {
|
|
28
|
+
readonly action: "repo.checkpoint.create";
|
|
29
|
+
readonly method: "POST";
|
|
30
|
+
readonly path: "/repo/:team/:repository/branches/:branch/checkpoints";
|
|
31
|
+
};
|
|
32
|
+
readonly open: {
|
|
33
|
+
readonly action: "mr.open";
|
|
34
|
+
readonly method: "POST";
|
|
35
|
+
readonly path: "/orbit/:team/:repository/merge-requests";
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/** A file on a branch, as a list shows it. */
|
|
39
|
+
interface BranchFile {
|
|
40
|
+
readonly path: string;
|
|
41
|
+
readonly size: number;
|
|
42
|
+
readonly executable: boolean;
|
|
43
|
+
readonly sealed: boolean;
|
|
44
|
+
readonly audience?: string;
|
|
45
|
+
}
|
|
46
|
+
/** One file read: its text, or none for a file that isn't text, and the snapshot it was read at. */
|
|
47
|
+
interface RepositoryFile {
|
|
48
|
+
readonly path: string;
|
|
49
|
+
readonly snapshot: string;
|
|
50
|
+
readonly size: number;
|
|
51
|
+
readonly text?: string;
|
|
52
|
+
readonly obliterated?: true;
|
|
53
|
+
}
|
|
54
|
+
/** What a search found: each file with its lines, the matched ones marked, and whether the matches were cut off. */
|
|
55
|
+
interface CodeSearch {
|
|
56
|
+
readonly snapshot: string;
|
|
57
|
+
readonly files: ReadonlyArray<{
|
|
58
|
+
readonly path: string;
|
|
59
|
+
readonly lines: ReadonlyArray<{
|
|
60
|
+
readonly number: number;
|
|
61
|
+
readonly text: string;
|
|
62
|
+
readonly matched: boolean;
|
|
63
|
+
}>;
|
|
64
|
+
}>;
|
|
65
|
+
readonly matches: number;
|
|
66
|
+
readonly truncated: boolean;
|
|
67
|
+
}
|
|
68
|
+
/** Changes saved to a view as one checkpoint. At least one file, edit, deletion or rename. */
|
|
69
|
+
interface Changes {
|
|
70
|
+
/** What the checkpoint changes, in a line. */
|
|
71
|
+
readonly message: string;
|
|
72
|
+
/** For a view that doesn't exist yet, the branch it starts from; the default branch when left out. */
|
|
73
|
+
readonly from?: string;
|
|
74
|
+
/** The snapshot the files were read at, as `list` and `read` give it: needed to save whole files, deletions or renames. */
|
|
75
|
+
readonly expectedSnapshot?: string;
|
|
76
|
+
readonly files?: ReadonlyArray<{
|
|
77
|
+
readonly path: string;
|
|
78
|
+
readonly text?: string;
|
|
79
|
+
readonly base64?: string;
|
|
80
|
+
readonly executable?: boolean;
|
|
81
|
+
}>;
|
|
82
|
+
/** Changes inside files: each `old` text is in its file exactly once and becomes `new`. */
|
|
83
|
+
readonly edits?: ReadonlyArray<{
|
|
84
|
+
readonly path: string;
|
|
85
|
+
readonly old: string;
|
|
86
|
+
readonly new: string;
|
|
87
|
+
}>;
|
|
88
|
+
readonly delete?: ReadonlyArray<string>;
|
|
89
|
+
readonly rename?: ReadonlyArray<{
|
|
90
|
+
readonly from: string;
|
|
91
|
+
readonly to: string;
|
|
92
|
+
}>;
|
|
93
|
+
/** Sent again with the same key within seven days, the first save is returned and nothing is saved twice. */
|
|
94
|
+
readonly key?: string;
|
|
95
|
+
}
|
|
96
|
+
/** A merge request to open from a view: into the default branch unless `target` names another. */
|
|
97
|
+
interface Proposal {
|
|
98
|
+
readonly source: string;
|
|
99
|
+
readonly title: string;
|
|
100
|
+
readonly target?: string;
|
|
101
|
+
readonly body?: string;
|
|
102
|
+
readonly draft?: boolean;
|
|
103
|
+
}
|
|
104
|
+
interface Repository {
|
|
105
|
+
/** The files on a branch or view, or as they were at one of its checkpoints (`at`). */
|
|
106
|
+
list(branch: string, options?: {
|
|
107
|
+
readonly at?: string;
|
|
108
|
+
}): Promise<{
|
|
109
|
+
readonly snapshot: string;
|
|
110
|
+
readonly files: ReadonlyArray<BranchFile>;
|
|
111
|
+
}>;
|
|
112
|
+
/** One file on a branch or view, or as it was at one of its checkpoints (`at`). */
|
|
113
|
+
read(branch: string, path: string, options?: {
|
|
114
|
+
readonly at?: string;
|
|
115
|
+
}): Promise<RepositoryFile>;
|
|
116
|
+
/** The lines of a branch's code that hold `text`; `path` and `exclude` are comma-separated patterns, such as src/*. */
|
|
117
|
+
search(branch: string, text: string, options?: {
|
|
118
|
+
readonly path?: string;
|
|
119
|
+
readonly exclude?: string;
|
|
120
|
+
readonly case?: "sensitive" | "insensitive";
|
|
121
|
+
readonly limit?: number;
|
|
122
|
+
}): Promise<CodeSearch>;
|
|
123
|
+
/** Saves `changes` to the view `view` as one checkpoint, and returns the view saved to and its new head. */
|
|
124
|
+
save(view: string, changes: Changes): Promise<{
|
|
125
|
+
readonly branch: string;
|
|
126
|
+
readonly snapshot: string;
|
|
127
|
+
}>;
|
|
128
|
+
/** Opens a merge request from a view, or returns the one open already from it into the same branch with the same title. */
|
|
129
|
+
open(proposal: Proposal): Promise<{
|
|
130
|
+
readonly number: number;
|
|
131
|
+
readonly mergeRequest: {
|
|
132
|
+
readonly id: string;
|
|
133
|
+
};
|
|
134
|
+
} & Readonly<Record<string, unknown>>>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The repository `name` (`<team>/<repository>`) through Midsummer's API, as the holder of `token`. `api` is the site's
|
|
138
|
+
* API; `fetch` replaces the global one, as a test's does.
|
|
139
|
+
*/
|
|
140
|
+
declare function repository(name: string, options: {
|
|
141
|
+
readonly token: string;
|
|
142
|
+
readonly api?: string;
|
|
143
|
+
readonly fetch?: typeof fetch;
|
|
144
|
+
}): Repository;
|
|
145
|
+
//#endregion
|
|
146
|
+
export { BranchFile, Changes, CodeSearch, Proposal, REPOSITORY_ACTIONS, Repository, RepositoryError, RepositoryErrorCode, RepositoryFile, repository };
|
package/repository.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { i as platformApi, t as clientHeaders } from "./here-BO-wQDjz.mjs";
|
|
2
|
+
//#region src/repository.ts
|
|
3
|
+
/**
|
|
4
|
+
* A Midsummer repository's files from code, as an agent works on them with no workspace: list, read and search a
|
|
5
|
+
* branch or view, save changes to a view as one checkpoint, and open a merge request for them. Each call is the Action
|
|
6
|
+
* the CLI, MCP and Midsummer's own agents call for it (`REPOSITORY_ACTIONS`), sent with a token whose person or agent
|
|
7
|
+
* may read or change the repository (`midsummer token create`).
|
|
8
|
+
*
|
|
9
|
+
* import { repository } from "@midsummer-ai/sdk/repository"
|
|
10
|
+
*
|
|
11
|
+
* const web = repository("acme/web", { token: process.env.MIDSUMMER_TOKEN! })
|
|
12
|
+
* const readme = await web.read("main", "README.md") // { path, snapshot, size, text }
|
|
13
|
+
* await web.save("say-hello", { message: "Say hello", from: "main", edits: [{ path: "README.md", old: "Hi", new: "Hello" }] })
|
|
14
|
+
* await web.open({ source: "say-hello", title: "Say hello" })
|
|
15
|
+
*
|
|
16
|
+
* The default branch takes no saves: save to a view, which `from` starts from a branch the first time. An edit's `old`
|
|
17
|
+
* text must be in its file exactly once. Whole files, deletions and renames are held to the `snapshot` they were read
|
|
18
|
+
* at (`expectedSnapshot`), and a save whose files changed since is refused as `conflict`. Calls go to `api`, or to
|
|
19
|
+
* `MIDSUMMER_API` when left out, else the API of the site this package was published for. Every failure is a
|
|
20
|
+
* `RepositoryError`, whose `code` shows which kind.
|
|
21
|
+
*/
|
|
22
|
+
var RepositoryError = class extends Error {
|
|
23
|
+
code;
|
|
24
|
+
constructor(code, message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.name = "RepositoryError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The Action each call is, by its method and its path under `/v1`, as `@midsummer/actions` declares it; a test keeps the
|
|
32
|
+
* two the same, so this door never drifts from the others.
|
|
33
|
+
*/
|
|
34
|
+
const REPOSITORY_ACTIONS = {
|
|
35
|
+
list: {
|
|
36
|
+
action: "repo.file.list",
|
|
37
|
+
method: "GET",
|
|
38
|
+
path: "/repo/:team/:repository/branches/:branch/files"
|
|
39
|
+
},
|
|
40
|
+
read: {
|
|
41
|
+
action: "repo.file.show",
|
|
42
|
+
method: "GET",
|
|
43
|
+
path: "/repo/:team/:repository/branches/:branch/file"
|
|
44
|
+
},
|
|
45
|
+
search: {
|
|
46
|
+
action: "repo.search",
|
|
47
|
+
method: "GET",
|
|
48
|
+
path: "/orbit/:team/:repository/branches/:branch/search"
|
|
49
|
+
},
|
|
50
|
+
save: {
|
|
51
|
+
action: "repo.checkpoint.create",
|
|
52
|
+
method: "POST",
|
|
53
|
+
path: "/repo/:team/:repository/branches/:branch/checkpoints"
|
|
54
|
+
},
|
|
55
|
+
open: {
|
|
56
|
+
action: "mr.open",
|
|
57
|
+
method: "POST",
|
|
58
|
+
path: "/orbit/:team/:repository/merge-requests"
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const CODES = {
|
|
62
|
+
400: "invalid",
|
|
63
|
+
401: "forbidden",
|
|
64
|
+
403: "forbidden",
|
|
65
|
+
404: "not-found",
|
|
66
|
+
409: "conflict",
|
|
67
|
+
422: "invalid"
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The repository `name` (`<team>/<repository>`) through Midsummer's API, as the holder of `token`. `api` is the site's
|
|
71
|
+
* API; `fetch` replaces the global one, as a test's does.
|
|
72
|
+
*/
|
|
73
|
+
function repository(name, options) {
|
|
74
|
+
const [team, repo, ...rest] = name.split("/");
|
|
75
|
+
if (!team || !repo || rest.length) throw new RepositoryError("invalid", "Name the repository as <team>/<repository>, such as acme/web.");
|
|
76
|
+
const send = options.fetch ?? fetch;
|
|
77
|
+
const call = async (which, branch, fields) => {
|
|
78
|
+
const { method, path } = REPOSITORY_ACTIONS[which];
|
|
79
|
+
const names = {
|
|
80
|
+
team,
|
|
81
|
+
repository: repo,
|
|
82
|
+
branch
|
|
83
|
+
};
|
|
84
|
+
const filled = path.replace(/:(\w+)/g, (_match, param) => encodeURIComponent(names[param] ?? ""));
|
|
85
|
+
const given = Object.entries(fields).filter(([, value]) => value !== void 0);
|
|
86
|
+
const query = method === "GET" && given.length ? `?${new URLSearchParams(given.map(([key, value]) => [key, String(value)]))}` : "";
|
|
87
|
+
let response;
|
|
88
|
+
try {
|
|
89
|
+
response = await send(`${(options.api ?? platformApi()).replace(/\/+$/, "")}/v1${filled}${query}`, {
|
|
90
|
+
method,
|
|
91
|
+
headers: {
|
|
92
|
+
authorization: `Bearer ${options.token}`,
|
|
93
|
+
...clientHeaders("repository"),
|
|
94
|
+
...method === "GET" ? {} : { "content-type": "application/json" }
|
|
95
|
+
},
|
|
96
|
+
...method === "GET" ? {} : { body: JSON.stringify(Object.fromEntries(given)) }
|
|
97
|
+
});
|
|
98
|
+
} catch {
|
|
99
|
+
throw new RepositoryError("unavailable", "Midsummer couldn't be reached.");
|
|
100
|
+
}
|
|
101
|
+
const answer = await response.json().catch(() => void 0);
|
|
102
|
+
if (response.ok) return answer;
|
|
103
|
+
const said = answer?.error;
|
|
104
|
+
const message = typeof said === "string" ? said : said?.message;
|
|
105
|
+
throw new RepositoryError(CODES[response.status] ?? "unavailable", message ?? `Midsummer returned ${response.status}.`);
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
list: (branch, given = {}) => call("list", branch, { at: given.at }),
|
|
109
|
+
read: (branch, path, given = {}) => call("read", branch, {
|
|
110
|
+
path,
|
|
111
|
+
at: given.at
|
|
112
|
+
}),
|
|
113
|
+
search: (branch, text, given = {}) => call("search", branch, {
|
|
114
|
+
q: text,
|
|
115
|
+
path: given.path,
|
|
116
|
+
exclude: given.exclude,
|
|
117
|
+
case: given.case,
|
|
118
|
+
limit: given.limit
|
|
119
|
+
}),
|
|
120
|
+
save: (view, changes) => call("save", view, { ...changes }),
|
|
121
|
+
open: (proposal) => call("open", void 0, { ...proposal })
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//#endregion
|
|
125
|
+
export { REPOSITORY_ACTIONS, RepositoryError, repository };
|