@openshain/agent 0.2.0 → 0.4.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.
- package/NOTICE +4 -0
- package/dist/client.d.ts +24 -0
- package/dist/client.js +48 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/session.d.ts +84 -16
- package/dist/session.js +622 -339
- package/package.json +7 -4
- package/src/client.ts +73 -0
- package/src/index.ts +5 -12
- package/src/session.ts +761 -397
- package/dist/loop.d.ts +0 -37
- package/dist/loop.js +0 -382
- package/src/loop.ts +0 -479
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openshain/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Tool loop and model providers (bring your own key)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openshain",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"!src/**/*.test.ts",
|
|
31
31
|
"!src/**/*.test.tsx",
|
|
32
32
|
"README.md",
|
|
33
|
-
"LICENSE"
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"NOTICE"
|
|
34
35
|
],
|
|
35
36
|
"exports": {
|
|
36
37
|
".": {
|
|
@@ -50,11 +51,13 @@
|
|
|
50
51
|
},
|
|
51
52
|
"dependencies": {
|
|
52
53
|
"@anthropic-ai/sdk": "0.123.0",
|
|
53
|
-
"@
|
|
54
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
55
|
+
"@openshain/core": "0.4.0",
|
|
54
56
|
"openai": "7.10.0"
|
|
55
57
|
},
|
|
56
58
|
"devDependencies": {
|
|
57
|
-
"@openshain/
|
|
59
|
+
"@openshain/mcp": "0.4.0",
|
|
60
|
+
"@openshain/tools": "0.4.0"
|
|
58
61
|
},
|
|
59
62
|
"publishConfig": {
|
|
60
63
|
"access": "public"
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
3
|
+
import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import type { JsonSchema, ToolContent, ToolDefinition } from "@openshain/core";
|
|
5
|
+
import pkg from "../package.json" with { type: "json" };
|
|
6
|
+
|
|
7
|
+
/** What a tool call returned, as the client sees it: MCP content, and the same as text. */
|
|
8
|
+
export interface ClientResult {
|
|
9
|
+
content: ToolContent[];
|
|
10
|
+
isError: boolean;
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The runtime as a client sees it: the tools it offers and a way to call them. The interactive
|
|
16
|
+
* CLI's loop talks to the runtime through this and nothing else, the way Claude Code does over MCP.
|
|
17
|
+
*/
|
|
18
|
+
export interface RuntimeClient {
|
|
19
|
+
listTools(): Promise<ToolDefinition[]>;
|
|
20
|
+
call(name: string, input: unknown, signal?: AbortSignal): Promise<ClientResult>;
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Connects an MCP client to a server in the same process, over the SDK's in-memory transport. */
|
|
25
|
+
export async function connectInMemory(server: Server): Promise<RuntimeClient> {
|
|
26
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
27
|
+
await server.connect(serverTransport);
|
|
28
|
+
const client = new Client({ name: "openshain", version: pkg.version });
|
|
29
|
+
await client.connect(clientTransport);
|
|
30
|
+
return wrap(client);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Adapts any connected MCP client to the runtime client the loop uses. */
|
|
34
|
+
export function wrap(client: Client): RuntimeClient {
|
|
35
|
+
return {
|
|
36
|
+
async listTools() {
|
|
37
|
+
const { tools } = await client.listTools();
|
|
38
|
+
return tools.map((tool) => ({
|
|
39
|
+
name: tool.name,
|
|
40
|
+
description: tool.description ?? "",
|
|
41
|
+
inputSchema: tool.inputSchema as JsonSchema,
|
|
42
|
+
effect: tool.annotations?.readOnlyHint === true ? "observe" : "mutate",
|
|
43
|
+
}));
|
|
44
|
+
},
|
|
45
|
+
async call(name, input, signal) {
|
|
46
|
+
const result = await client.callTool(
|
|
47
|
+
{ name, arguments: (input ?? {}) as Record<string, unknown> },
|
|
48
|
+
undefined,
|
|
49
|
+
signal ? { signal } : undefined,
|
|
50
|
+
);
|
|
51
|
+
const parts = (result.content ?? []) as { type: string; text?: string }[];
|
|
52
|
+
const content: ToolContent[] = parts.map((part) => ({
|
|
53
|
+
type: "text",
|
|
54
|
+
text: part.type === "text" ? (part.text ?? "") : JSON.stringify(part),
|
|
55
|
+
}));
|
|
56
|
+
return {
|
|
57
|
+
content,
|
|
58
|
+
isError: result.isError === true,
|
|
59
|
+
text: content.map((c) => (c.type === "text" ? c.text : "")).join(""),
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
close: () => client.close(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Parses a JSON result. Returns undefined when the text is not JSON. */
|
|
67
|
+
export function jsonOf(result: ClientResult): unknown {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(result.text);
|
|
70
|
+
} catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
// @openshain/agent:
|
|
1
|
+
// @openshain/agent: The conversation loop that drives the runtime as an MCP client, and the model providers (bring your own key)
|
|
2
2
|
|
|
3
|
-
export {
|
|
4
|
-
ASK_USER,
|
|
5
|
-
countToolCalls,
|
|
6
|
-
type FailureReason,
|
|
7
|
-
type PendingQuestion,
|
|
8
|
-
pendingQuestions,
|
|
9
|
-
RUNTIME_PROVIDER_ID,
|
|
10
|
-
type RunWorkOptions,
|
|
11
|
-
runWork,
|
|
12
|
-
} from "./loop.ts";
|
|
3
|
+
export { type ClientResult, connectInMemory, jsonOf, type RuntimeClient, wrap } from "./client.ts";
|
|
13
4
|
export { AGENT_NAMES, pickAgentName } from "./names.ts";
|
|
14
5
|
export {
|
|
15
6
|
ANTHROPIC_PROVIDER_ID,
|
|
@@ -24,8 +15,10 @@ export {
|
|
|
24
15
|
openaiCompatibleProvider,
|
|
25
16
|
} from "./providers/openai-compatible.ts";
|
|
26
17
|
export {
|
|
18
|
+
type ApprovalAnswer,
|
|
19
|
+
type ApprovalChoice,
|
|
27
20
|
createSession,
|
|
28
|
-
|
|
21
|
+
type HeldApproval,
|
|
29
22
|
type Session,
|
|
30
23
|
type SessionOptions,
|
|
31
24
|
TURN_LIMITS,
|