@midsummer-ai/sdk 0.0.0-stage → 0.1.0-next.1723
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/LICENSE +177 -0
- package/README.md +4 -2
- package/agents/contract.d.mts +9 -0
- package/agents/contract.mjs +221 -0
- package/agents/index.d.mts +128 -0
- package/agents/index.mjs +203 -0
- package/agents/stand-in.d.mts +35 -0
- package/agents/stand-in.mjs +149 -0
- package/ai/contract.d.mts +10 -0
- package/ai/contract.mjs +77 -0
- package/ai/index.d.mts +94 -0
- package/ai/index.mjs +193 -0
- package/ai/stand-in.d.mts +7 -0
- package/ai/stand-in.mjs +41 -0
- package/build-SQ44jwSa.d.mts +41 -0
- package/client.d.mts +45 -0
- package/client.mjs +106 -0
- package/connection-BHjDaLYA.mjs +2009 -0
- package/contract.d.mts +10 -0
- package/contract.mjs +469 -0
- package/database/build.d.mts +2 -0
- package/database/build.mjs +579 -0
- package/database/client.d.mts +60 -0
- package/database/client.mjs +133 -0
- package/database/contract.d.mts +11 -0
- package/database/contract.mjs +164 -0
- package/database/errors.d.mts +23 -0
- package/database/errors.mjs +13 -0
- package/database/server.d.mts +3 -0
- package/database/server.mjs +320 -0
- package/database/stand-in.d.mts +99 -0
- package/database/stand-in.mjs +430 -0
- package/email/client.d.mts +34 -0
- package/email/client.mjs +131 -0
- package/email/contract.d.mts +11 -0
- package/email/contract.mjs +99 -0
- package/email/index.d.mts +74 -0
- package/email/index.mjs +59 -0
- package/email/smtp.d.mts +22 -0
- package/email/smtp.mjs +296 -0
- package/email/stand-in.d.mts +13 -0
- package/email/stand-in.mjs +51 -0
- package/index-DdoK0y7D.d.mts +192 -0
- package/index-sgdyoA2P.d.mts +212 -0
- package/index.d.mts +2 -0
- package/index.mjs +137 -0
- package/names-DfCBKupJ.mjs +115 -0
- package/package.json +152 -4
- package/predict-Drfm_8pO.mjs +179 -0
- package/queues/contract.d.mts +13 -0
- package/queues/contract.mjs +278 -0
- package/queues/index.d.mts +184 -0
- package/queues/index.mjs +126 -0
- package/queues/stand-in.d.mts +7 -0
- package/queues/stand-in.mjs +110 -0
- package/queues/wire.d.mts +9 -0
- package/queues/wire.mjs +199 -0
- package/s3.d.mts +43 -0
- package/s3.mjs +1003 -0
- package/schema-BF-Y1EVu.mjs +142 -0
- package/schema-DS8x1Dn8.mjs +115 -0
- package/schema-f7E9uzUn.d.mts +74 -0
- package/server-C7O7CICU.d.mts +182 -0
- package/socket.d.mts +67 -0
- package/socket.mjs +263 -0
- package/stand-in.d.mts +7 -0
- package/stand-in.mjs +185 -0
- package/store-BA8K-bAR.mjs +237 -0
- package/tabs-DEeMee7M.mjs +457 -0
- package/vector/contract.d.mts +10 -0
- package/vector/contract.mjs +635 -0
- package/vector/index.d.mts +2 -0
- package/vector/index.mjs +108 -0
- package/vector/stand-in.d.mts +11 -0
- package/vector/stand-in.mjs +169 -0
- package/vector/wire.d.mts +25 -0
- package/vector/wire.mjs +175 -0
- package/wire.d.mts +54 -0
- package/wire.mjs +731 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @midsummer-ai/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The handles a Midsummer app's code uses for its Database, Storage, Queues, Vector DB, AI, Email and Agents, each with a stand-in for tests.
|
|
4
|
+
|
|
5
|
+
Docs: https://docs.midsummer.new
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RunWire } from "./index.mjs";
|
|
2
|
+
//#region src/agents/contract.d.ts
|
|
3
|
+
interface AgentContractCase {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
run(start: (input?: unknown) => Promise<RunWire>): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
declare const agentContract: ReadonlyArray<AgentContractCase>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { AgentContractCase, agentContract };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { AgentError, execute } from "./index.mjs";
|
|
2
|
+
//#region src/agents/contract.ts
|
|
3
|
+
/**
|
|
4
|
+
* Agents' contract: what every place that keeps Agent runs promises, as cases any test runner can
|
|
5
|
+
* run. The Stand-in runs them in this package; the platform runs the same cases through its wire.
|
|
6
|
+
*
|
|
7
|
+
* for (const c of agentContract) test(c.name, () => c.run(makeRuns()))
|
|
8
|
+
*
|
|
9
|
+
* Each case gets `start`, which begins a new run of a fresh Agent whose version allows the tool
|
|
10
|
+
* `greet` and no other, in one Environment, and returns the run's wire. Expected values are written
|
|
11
|
+
* out, never computed with the SDK's own code.
|
|
12
|
+
*/
|
|
13
|
+
const greeter = {
|
|
14
|
+
tools: { greet: (input) => `Hello, ${input.name}!` },
|
|
15
|
+
async run(input, agent) {
|
|
16
|
+
await agent.log("starting");
|
|
17
|
+
return { greeting: await agent.tool("greet", { name: input.name }) };
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const agentContract = [
|
|
21
|
+
{
|
|
22
|
+
name: "a run completes with what its code returns, and a run that ended is not run again",
|
|
23
|
+
async run(start) {
|
|
24
|
+
const wire = await start({ name: "Ada" });
|
|
25
|
+
equal(await execute(greeter, wire), {
|
|
26
|
+
status: "completed",
|
|
27
|
+
output: { greeting: "Hello, Ada!" }
|
|
28
|
+
}, "the run's end");
|
|
29
|
+
let ran = false;
|
|
30
|
+
equal(await execute({ run: () => {
|
|
31
|
+
ran = true;
|
|
32
|
+
} }, wire), {
|
|
33
|
+
status: "completed",
|
|
34
|
+
output: { greeting: "Hello, Ada!" }
|
|
35
|
+
}, "the end, asked again");
|
|
36
|
+
equal(ran, false, "whether the code ran again");
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "a run whose code throws fails with its message",
|
|
41
|
+
async run(start) {
|
|
42
|
+
equal(await execute({ run: () => {
|
|
43
|
+
throw new Error("no input");
|
|
44
|
+
} }, await start()), {
|
|
45
|
+
status: "failed",
|
|
46
|
+
error: "no input"
|
|
47
|
+
}, "the run's end");
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "a run cut off part way resumes when run again: a step's work is done once",
|
|
52
|
+
async run(start) {
|
|
53
|
+
const wire = await start({ name: "Ada" });
|
|
54
|
+
let charged = 0;
|
|
55
|
+
const agent = { async run(_input, context) {
|
|
56
|
+
await context.step("charge", () => {
|
|
57
|
+
charged += 1;
|
|
58
|
+
return "charged";
|
|
59
|
+
});
|
|
60
|
+
await context.memory.set("charged", true);
|
|
61
|
+
return { done: await context.step("send", () => "sent") };
|
|
62
|
+
} };
|
|
63
|
+
let calls = 0;
|
|
64
|
+
const cut = {
|
|
65
|
+
...wire,
|
|
66
|
+
call: (number, call) => ++calls > 3 ? Promise.reject(new AgentError("unavailable", "lost")) : wire.call(number, call),
|
|
67
|
+
finish: () => Promise.reject(new AgentError("unavailable", "lost"))
|
|
68
|
+
};
|
|
69
|
+
await execute(agent, cut).then(() => {
|
|
70
|
+
throw new Error("the cut-off run ended");
|
|
71
|
+
}, (error) => equal(error.code, "unavailable", "why it stopped"));
|
|
72
|
+
equal(await execute(agent, wire), {
|
|
73
|
+
status: "completed",
|
|
74
|
+
output: { done: "sent" }
|
|
75
|
+
}, "the resumed run's end");
|
|
76
|
+
equal(charged, 1, "how many times the step's work ran");
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "a tool whose own work made calls is answered from what was kept, and the run goes on after them",
|
|
81
|
+
async run(start) {
|
|
82
|
+
const wire = await start({ name: "Ada" });
|
|
83
|
+
let greeted = 0;
|
|
84
|
+
const agent = {
|
|
85
|
+
tools: { greet: async (input, context) => {
|
|
86
|
+
greeted += 1;
|
|
87
|
+
await context.log(`greeting ${input.name}`);
|
|
88
|
+
return `Hello, ${input.name}!`;
|
|
89
|
+
} },
|
|
90
|
+
async run(input, context) {
|
|
91
|
+
const greeting = await context.tool("greet", { name: input.name });
|
|
92
|
+
await context.memory.set("greeted", greeting);
|
|
93
|
+
return {
|
|
94
|
+
greeting,
|
|
95
|
+
kept: await context.memory.get("greeted")
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
let calls = 0;
|
|
100
|
+
const cut = {
|
|
101
|
+
...wire,
|
|
102
|
+
call: (number, call) => ++calls > 3 ? Promise.reject(new AgentError("unavailable", "lost")) : wire.call(number, call),
|
|
103
|
+
finish: () => Promise.reject(new AgentError("unavailable", "lost"))
|
|
104
|
+
};
|
|
105
|
+
await execute(agent, cut).then(() => {
|
|
106
|
+
throw new Error("the cut-off run ended");
|
|
107
|
+
}, (error) => equal(error.code, "unavailable", "why it stopped"));
|
|
108
|
+
equal(await execute(agent, wire), {
|
|
109
|
+
status: "completed",
|
|
110
|
+
output: {
|
|
111
|
+
greeting: "Hello, Ada!",
|
|
112
|
+
kept: "Hello, Ada!"
|
|
113
|
+
}
|
|
114
|
+
}, "the resumed run's end");
|
|
115
|
+
equal(greeted, 1, "how many times the tool ran");
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: "steps run side by side, one kept and one not when the run was cut off, each resume where they were",
|
|
120
|
+
async run(start) {
|
|
121
|
+
const wire = await start();
|
|
122
|
+
let ran = 0;
|
|
123
|
+
let read = () => {}, release = () => {};
|
|
124
|
+
const slowRead = new Promise((resolve) => {
|
|
125
|
+
read = resolve;
|
|
126
|
+
});
|
|
127
|
+
const slowGoesOn = new Promise((resolve) => {
|
|
128
|
+
release = resolve;
|
|
129
|
+
});
|
|
130
|
+
let cutting = true;
|
|
131
|
+
const agent = { async run(_input, context) {
|
|
132
|
+
const [fast, slow] = await Promise.all([context.step("fast", async () => {
|
|
133
|
+
ran += 1;
|
|
134
|
+
await slowRead;
|
|
135
|
+
await context.memory.set("counter", 1);
|
|
136
|
+
return "fast done";
|
|
137
|
+
}), context.step("slow", async () => {
|
|
138
|
+
const seen = await context.memory.get("counter");
|
|
139
|
+
read();
|
|
140
|
+
if (cutting) await slowGoesOn;
|
|
141
|
+
return seen;
|
|
142
|
+
})]);
|
|
143
|
+
return {
|
|
144
|
+
fast,
|
|
145
|
+
slow
|
|
146
|
+
};
|
|
147
|
+
} };
|
|
148
|
+
const cut = {
|
|
149
|
+
...wire,
|
|
150
|
+
call: async (id, call) => {
|
|
151
|
+
if (!cutting) throw new AgentError("unavailable", "lost");
|
|
152
|
+
const answer = await wire.call(id, call);
|
|
153
|
+
if (call.kind === "step" && call.name === "fast" && call.done) {
|
|
154
|
+
cutting = false;
|
|
155
|
+
release();
|
|
156
|
+
}
|
|
157
|
+
return answer;
|
|
158
|
+
},
|
|
159
|
+
finish: () => Promise.reject(new AgentError("unavailable", "lost"))
|
|
160
|
+
};
|
|
161
|
+
await execute(agent, cut).then(() => {
|
|
162
|
+
throw new Error("the cut-off run ended");
|
|
163
|
+
}, (error) => equal(error.code, "unavailable", "why it stopped"));
|
|
164
|
+
equal(await execute(agent, wire), {
|
|
165
|
+
status: "completed",
|
|
166
|
+
output: {
|
|
167
|
+
fast: "fast done",
|
|
168
|
+
slow: null
|
|
169
|
+
}
|
|
170
|
+
}, "the resumed run's end");
|
|
171
|
+
equal(ran, 1, "how many times the kept step's work ran");
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
name: "a tool the version does not allow is refused, and the run can go on",
|
|
176
|
+
async run(start) {
|
|
177
|
+
equal(await execute({
|
|
178
|
+
tools: {
|
|
179
|
+
greet: () => "hi",
|
|
180
|
+
wipe: () => "wiped"
|
|
181
|
+
},
|
|
182
|
+
async run(_input, context) {
|
|
183
|
+
return {
|
|
184
|
+
refused: await context.tool("wipe").then(() => "ran", (error) => error.code),
|
|
185
|
+
greet: await context.tool("greet")
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}, await start()), {
|
|
189
|
+
status: "completed",
|
|
190
|
+
output: {
|
|
191
|
+
refused: "refused",
|
|
192
|
+
greet: "hi"
|
|
193
|
+
}
|
|
194
|
+
}, "the run's end");
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: "memory one run writes, the next run in the Environment reads",
|
|
199
|
+
async run(start) {
|
|
200
|
+
await execute({ async run(_input, context) {
|
|
201
|
+
await context.memory.set("seen", ["Ada"]);
|
|
202
|
+
} }, await start());
|
|
203
|
+
equal(await execute({ run: async (_input, context) => ({
|
|
204
|
+
seen: await context.memory.get("seen"),
|
|
205
|
+
none: await context.memory.get("nothing")
|
|
206
|
+
}) }, await start()), {
|
|
207
|
+
status: "completed",
|
|
208
|
+
output: {
|
|
209
|
+
seen: ["Ada"],
|
|
210
|
+
none: null
|
|
211
|
+
}
|
|
212
|
+
}, "the second run's end");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
];
|
|
216
|
+
function equal(actual, expected, what) {
|
|
217
|
+
const a = JSON.stringify(actual), b = JSON.stringify(expected);
|
|
218
|
+
if (a !== b) throw new Error(`${what}: expected ${b}, got ${a}`);
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
export { agentContract };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/agents/index.d.ts
|
|
2
|
+
/** What a run's code reaches through its `agent`. */
|
|
3
|
+
interface AgentContext {
|
|
4
|
+
readonly runId: string;
|
|
5
|
+
/** The Agent's name. */
|
|
6
|
+
readonly agent: string;
|
|
7
|
+
readonly environment: string;
|
|
8
|
+
readonly version: number;
|
|
9
|
+
/** The version's instructions, for model questions. */
|
|
10
|
+
readonly instructions: string;
|
|
11
|
+
/** The run's Environment values, and a placeholder for each secret bound to hosts. */
|
|
12
|
+
readonly env: Readonly<Record<string, string>>;
|
|
13
|
+
/** The tools the version allows. */
|
|
14
|
+
readonly tools: ReadonlyArray<string>;
|
|
15
|
+
/** A line in the run's events. */
|
|
16
|
+
log(message: string, data?: unknown): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Runs `work` and records its result; when the run resumes, a completed step returns its recorded
|
|
19
|
+
* result without running again. A step interrupted before its result is recorded can run again,
|
|
20
|
+
* so make outside writes idempotent with an identifier that stays the same across attempts.
|
|
21
|
+
*/
|
|
22
|
+
step<T>(name: string, work: () => T | Promise<T>): Promise<T>;
|
|
23
|
+
/** Calls one of the Agent's tools, when the version allows it; refused otherwise. Recorded as a step is, so the same rule holds: an interrupted call can run again. */
|
|
24
|
+
tool(name: string, input?: unknown): Promise<unknown>;
|
|
25
|
+
/** The Environment's memory: kept between runs. */
|
|
26
|
+
readonly memory: {
|
|
27
|
+
get(key: string): Promise<unknown>;
|
|
28
|
+
set(key: string, value: unknown): Promise<void>;
|
|
29
|
+
delete(key: string): Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
/** Keeps a file with the run, `text/plain` unless typed. */
|
|
32
|
+
artifact(name: string, content: string | Uint8Array, type?: string): Promise<void>;
|
|
33
|
+
/** Asks the model, with the version's instructions; what it costs counts toward the version's spend ceiling. */
|
|
34
|
+
ask(prompt: string): Promise<string>;
|
|
35
|
+
}
|
|
36
|
+
/** An Agent: its `run`, and the tools it may be allowed. */
|
|
37
|
+
interface Agent<Input = any, Output = unknown> {
|
|
38
|
+
readonly tools?: Readonly<Record<string, (input: any, agent: AgentContext) => unknown>>;
|
|
39
|
+
run(input: Input, agent: AgentContext): Output | Promise<Output>;
|
|
40
|
+
}
|
|
41
|
+
/** One call a run's code makes, as its wire keeps it. */
|
|
42
|
+
type AgentCall = {
|
|
43
|
+
readonly kind: "log";
|
|
44
|
+
readonly message: string;
|
|
45
|
+
readonly data?: unknown;
|
|
46
|
+
} | {
|
|
47
|
+
readonly kind: "step";
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly done?: boolean;
|
|
50
|
+
readonly value?: unknown;
|
|
51
|
+
} | {
|
|
52
|
+
readonly kind: "tool";
|
|
53
|
+
readonly name: string;
|
|
54
|
+
readonly input?: unknown;
|
|
55
|
+
readonly done?: boolean;
|
|
56
|
+
readonly value?: unknown;
|
|
57
|
+
} | {
|
|
58
|
+
readonly kind: "memory.get";
|
|
59
|
+
readonly key: string;
|
|
60
|
+
} | {
|
|
61
|
+
readonly kind: "memory.set";
|
|
62
|
+
readonly key: string;
|
|
63
|
+
readonly value: unknown;
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: "memory.delete";
|
|
66
|
+
readonly key: string;
|
|
67
|
+
} | {
|
|
68
|
+
readonly kind: "artifact";
|
|
69
|
+
readonly name: string;
|
|
70
|
+
readonly type: string;
|
|
71
|
+
readonly data: string;
|
|
72
|
+
} | {
|
|
73
|
+
readonly kind: "ask";
|
|
74
|
+
readonly prompt: string;
|
|
75
|
+
};
|
|
76
|
+
type RunStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
|
|
77
|
+
/** What a run is when it begins. */
|
|
78
|
+
interface RunStart {
|
|
79
|
+
readonly runId: string;
|
|
80
|
+
readonly agent: string;
|
|
81
|
+
readonly environment: string;
|
|
82
|
+
readonly version: number;
|
|
83
|
+
readonly input: unknown;
|
|
84
|
+
readonly instructions: string;
|
|
85
|
+
readonly tools: ReadonlyArray<string>;
|
|
86
|
+
readonly status: RunStatus;
|
|
87
|
+
readonly output?: unknown;
|
|
88
|
+
readonly error?: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Where a run is kept. `call` keeps the call with id `id` and replies to it, or returns what it kept
|
|
92
|
+
* before; a step or tool asked without `done` returns `{ done: false }` until its value is kept,
|
|
93
|
+
* then `{ done: true, value }`. A call's id is its place among its scope's calls, from 0, after the
|
|
94
|
+
* ids of the steps it is in: the run's own calls are `0`, `1`, …, and step 3's work makes `3.0`,
|
|
95
|
+
* `3.1`, …, whatever runs beside it. It throws `AgentError`; `unavailable` means ask again later.
|
|
96
|
+
*/
|
|
97
|
+
interface RunWire {
|
|
98
|
+
begin(): Promise<RunStart>;
|
|
99
|
+
call(id: string, call: AgentCall): Promise<unknown>;
|
|
100
|
+
finish(end: {
|
|
101
|
+
readonly output: unknown;
|
|
102
|
+
} | {
|
|
103
|
+
readonly error: string;
|
|
104
|
+
}): Promise<unknown>;
|
|
105
|
+
}
|
|
106
|
+
type AgentErrorCode = "refused" | "ended" | "invalid" | "not-found" | "unavailable";
|
|
107
|
+
declare class AgentError extends Error {
|
|
108
|
+
readonly code: AgentErrorCode;
|
|
109
|
+
constructor(code: AgentErrorCode, message: string);
|
|
110
|
+
}
|
|
111
|
+
/** How a run ended. */
|
|
112
|
+
interface RunEnd {
|
|
113
|
+
readonly status: RunStatus;
|
|
114
|
+
readonly output?: unknown;
|
|
115
|
+
readonly error?: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Runs one Agent run to its end and returns how it ended. A run that already ended is not run
|
|
119
|
+
* again. When the wire itself fails, so the run can't be kept, it throws, and the run waits to be
|
|
120
|
+
* executed again.
|
|
121
|
+
*/
|
|
122
|
+
declare function execute(agent: Agent, wire: RunWire, env?: Readonly<Record<string, string>>): Promise<RunEnd>;
|
|
123
|
+
/** The wire of a run on the platform, at `url` with the run's token. */
|
|
124
|
+
declare function httpWire(url: string, token: string, fetcher?: typeof fetch): RunWire;
|
|
125
|
+
/** Runs the run the environment names, as the platform's host does: `MIDSUMMER_AGENT_WIRE` and `MIDSUMMER_AGENT_TOKEN`; the rest is the run's `env`. */
|
|
126
|
+
declare function executeFromEnvironment(agent: Agent, env: Readonly<Record<string, string | undefined>>): Promise<RunEnd>;
|
|
127
|
+
//#endregion
|
|
128
|
+
export { Agent, AgentCall, AgentContext, AgentError, AgentErrorCode, RunEnd, RunStart, RunStatus, RunWire, execute, executeFromEnvironment, httpWire };
|