@tinyweb_dev/agent-framework-agent 0.2.0 → 0.3.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/dist/agent.d.ts +116 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/attachments.d.ts +41 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/bus/bus.d.ts +46 -0
- package/dist/bus/bus.d.ts.map +1 -0
- package/dist/bus/index.d.ts +2 -0
- package/dist/bus/index.d.ts.map +1 -0
- package/dist/define-tool.d.ts +13 -0
- package/dist/define-tool.d.ts.map +1 -0
- package/dist/events.d.ts +136 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/http-handlers.cjs +253 -0
- package/dist/http-handlers.cjs.map +1 -0
- package/dist/http-handlers.d.ts +49 -0
- package/dist/http-handlers.d.ts.map +1 -0
- package/dist/http-handlers.js +251 -0
- package/dist/http-handlers.js.map +1 -0
- package/dist/index.cjs +2073 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2015 -0
- package/dist/index.js.map +1 -0
- package/dist/loop-guard.d.ts +42 -0
- package/dist/loop-guard.d.ts.map +1 -0
- package/dist/mcp/client.d.ts +25 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/index.cjs +66 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/{src/mcp/index.ts → dist/mcp/index.d.ts} +2 -1
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +63 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/permission.d.ts +26 -0
- package/dist/permission.d.ts.map +1 -0
- package/dist/plugin/apply.d.ts +12 -0
- package/dist/plugin/apply.d.ts.map +1 -0
- package/dist/plugin/index.cjs +63 -0
- package/dist/plugin/index.cjs.map +1 -0
- package/dist/plugin/index.d.ts +4 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/index.js +52 -0
- package/dist/plugin/index.js.map +1 -0
- package/dist/plugin/loader.d.ts +3 -0
- package/dist/plugin/loader.d.ts.map +1 -0
- package/dist/plugin/types.d.ts +98 -0
- package/dist/plugin/types.d.ts.map +1 -0
- package/dist/prompt/index.d.ts +51 -0
- package/dist/prompt/index.d.ts.map +1 -0
- package/dist/prompt/objective.d.ts +29 -0
- package/dist/prompt/objective.d.ts.map +1 -0
- package/dist/prompt/response-style.d.ts +19 -0
- package/dist/prompt/response-style.d.ts.map +1 -0
- package/dist/prompt/tool-use-guidelines.d.ts +22 -0
- package/dist/prompt/tool-use-guidelines.d.ts.map +1 -0
- package/dist/prompt/tool-use.d.ts +12 -0
- package/dist/prompt/tool-use.d.ts.map +1 -0
- package/dist/runtime.d.ts +7 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/session/adapter.d.ts +40 -0
- package/dist/session/adapter.d.ts.map +1 -0
- package/dist/session/compaction.d.ts +37 -0
- package/dist/session/compaction.d.ts.map +1 -0
- package/dist/session/index.d.ts +7 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/memory.cjs +130 -0
- package/dist/session/memory.cjs.map +1 -0
- package/dist/session/memory.d.ts +3 -0
- package/dist/session/memory.d.ts.map +1 -0
- package/dist/session/memory.js +128 -0
- package/dist/session/memory.js.map +1 -0
- package/dist/session/overflow.d.ts +19 -0
- package/dist/session/overflow.d.ts.map +1 -0
- package/dist/session/postgres.cjs +245 -0
- package/dist/session/postgres.cjs.map +1 -0
- package/dist/session/postgres.d.ts +10 -0
- package/dist/session/postgres.d.ts.map +1 -0
- package/dist/session/postgres.js +243 -0
- package/dist/session/postgres.js.map +1 -0
- package/dist/session/processor.d.ts +52 -0
- package/dist/session/processor.d.ts.map +1 -0
- package/dist/session/replay.d.ts +4 -0
- package/dist/session/replay.d.ts.map +1 -0
- package/dist/session/schema.d.ts +104 -0
- package/dist/session/schema.d.ts.map +1 -0
- package/dist/session/sqlite.cjs +236 -0
- package/dist/session/sqlite.cjs.map +1 -0
- package/dist/session/sqlite.d.ts +3 -0
- package/dist/session/sqlite.d.ts.map +1 -0
- package/dist/session/sqlite.js +234 -0
- package/dist/session/sqlite.js.map +1 -0
- package/dist/session-api.cjs +56 -0
- package/dist/session-api.cjs.map +1 -0
- package/dist/session-api.d.ts +36 -0
- package/dist/session-api.d.ts.map +1 -0
- package/dist/session-api.js +54 -0
- package/dist/session-api.js.map +1 -0
- package/dist/skill/index.cjs +81 -0
- package/dist/skill/index.cjs.map +1 -0
- package/dist/skill/index.d.ts +10 -0
- package/dist/skill/index.d.ts.map +1 -0
- package/dist/skill/index.js +78 -0
- package/dist/skill/index.js.map +1 -0
- package/dist/sse-handler.d.ts +27 -0
- package/dist/sse-handler.d.ts.map +1 -0
- package/dist/transcript.d.ts +81 -0
- package/dist/transcript.d.ts.map +1 -0
- package/package.json +54 -19
- package/src/agent.ts +0 -470
- package/src/attachments.ts +0 -155
- package/src/bus/bus.ts +0 -89
- package/src/bus/index.ts +0 -1
- package/src/define-tool.ts +0 -41
- package/src/events.ts +0 -186
- package/src/http-handlers.ts +0 -251
- package/src/index.ts +0 -128
- package/src/loop-guard.ts +0 -99
- package/src/mcp/client.ts +0 -129
- package/src/permission.ts +0 -114
- package/src/plugin/apply.ts +0 -89
- package/src/plugin/index.ts +0 -3
- package/src/plugin/loader.ts +0 -42
- package/src/plugin/types.ts +0 -134
- package/src/prompt/index.ts +0 -90
- package/src/prompt/objective.ts +0 -104
- package/src/prompt/response-style.ts +0 -59
- package/src/prompt/tool-use-guidelines.ts +0 -57
- package/src/prompt/tool-use.ts +0 -27
- package/src/runtime.ts +0 -22
- package/src/session/adapter.ts +0 -46
- package/src/session/compaction.ts +0 -160
- package/src/session/index.ts +0 -18
- package/src/session/memory.ts +0 -169
- package/src/session/overflow.ts +0 -50
- package/src/session/postgres.ts +0 -355
- package/src/session/processor.ts +0 -865
- package/src/session/replay.ts +0 -151
- package/src/session/schema.ts +0 -156
- package/src/session/sqlite.ts +0 -309
- package/src/session-api.ts +0 -103
- package/src/skill/index.ts +0 -113
- package/src/sse-handler.ts +0 -67
- package/src/transcript.ts +0 -126
package/src/loop-guard.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { Tool, ToolFailure, type AnyExecutableTool, type ExecutableTools } from "@tinyweb-agent-framework/llm"
|
|
2
|
-
import { Effect } from "effect"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Loop-guard configuration. The guard wraps each tool in an
|
|
6
|
-
* `ExecutableTools` record so that identical `(toolName, JSON.stringify(args))`
|
|
7
|
-
* invocations short-circuit with a `ToolFailure` instead of running the
|
|
8
|
-
* tool body. This stops a runaway model that keeps re-invoking the same
|
|
9
|
-
* tool with the same args within a single agent turn (see
|
|
10
|
-
* [`.manager/bugs/0002-next-chat-agent-tool-loop-and-missing-stop-button.md`](../../../.manager/bugs/0002-next-chat-agent-tool-loop-and-missing-stop-button.md)).
|
|
11
|
-
*
|
|
12
|
-
* Scope:
|
|
13
|
-
* • The seen-set is **per `applyLoopGuard()` invocation**. Because
|
|
14
|
-
* `buildTools` runs once per `defineAgent`, and the typical pattern
|
|
15
|
-
* is one `defineAgent` per chat turn (e.g.
|
|
16
|
-
* [`examples/next-chat/lib/agent.ts`](../../../examples/next-chat/lib/agent.ts)),
|
|
17
|
-
* the seen-set naturally resets each turn.
|
|
18
|
-
* • For long-lived agents that span multiple turns (e.g. an HTTP server
|
|
19
|
-
* building the agent once at boot), the guard is currently
|
|
20
|
-
* conservative — duplicate calls across turns will also be blocked.
|
|
21
|
-
* If that becomes a problem we can add a turn-scoped reset hook.
|
|
22
|
-
*
|
|
23
|
-
* Behaviour on trip:
|
|
24
|
-
* • Returns `ToolFailure` with a clear message instructing the LLM to
|
|
25
|
-
* stop calling the tool. The processor surfaces this as a regular
|
|
26
|
-
* `tool-error` event so the UI renders an inline error pill (no
|
|
27
|
-
* stream crash).
|
|
28
|
-
*/
|
|
29
|
-
export type LoopGuardConfig = {
|
|
30
|
-
readonly enabled: boolean
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const DEFAULT_CONFIG: LoopGuardConfig = { enabled: true }
|
|
34
|
-
|
|
35
|
-
const tripFailure = (toolName: string): ToolFailure =>
|
|
36
|
-
new ToolFailure({
|
|
37
|
-
message:
|
|
38
|
-
`Loop guard: tool "${toolName}" was already invoked with identical arguments ` +
|
|
39
|
-
`earlier in this turn. Stop calling it and answer the user with the data ` +
|
|
40
|
-
`you already have.`,
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Stable JSON stringify with sorted keys so `{a:1,b:2}` and `{b:2,a:1}`
|
|
45
|
-
* produce the same signature. Falls back to `String(input)` for circular
|
|
46
|
-
* or non-serialisable inputs (which we still want to dedup on best-effort).
|
|
47
|
-
*/
|
|
48
|
-
const signature = (toolName: string, input: unknown): string => {
|
|
49
|
-
const safe = stableStringify(input)
|
|
50
|
-
return toolName + ":" + safe
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const stableStringify = (value: unknown): string => {
|
|
54
|
-
if (value === null || value === undefined) return "null"
|
|
55
|
-
if (typeof value !== "object") return JSON.stringify(value)
|
|
56
|
-
if (Array.isArray(value)) return "[" + value.map(stableStringify).join(",") + "]"
|
|
57
|
-
const obj = value as Record<string, unknown>
|
|
58
|
-
const keys = Object.keys(obj).sort()
|
|
59
|
-
return "{" + keys.map((k) => JSON.stringify(k) + ":" + stableStringify(obj[k])).join(",") + "}"
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const wrapTool = (name: string, base: AnyExecutableTool, seen: Set<string>): AnyExecutableTool =>
|
|
63
|
-
Tool.make({
|
|
64
|
-
description: base.description,
|
|
65
|
-
parameters: base.parameters,
|
|
66
|
-
success: base.success,
|
|
67
|
-
execute: (input: unknown) => {
|
|
68
|
-
const sig = signature(name, input)
|
|
69
|
-
if (seen.has(sig)) return Effect.fail(tripFailure(name))
|
|
70
|
-
seen.add(sig)
|
|
71
|
-
return base.execute(input)
|
|
72
|
-
},
|
|
73
|
-
}) as AnyExecutableTool
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Wrap every tool in `tools` with the loop guard. Returns the original
|
|
77
|
-
* record unchanged when `config.enabled === false`.
|
|
78
|
-
*
|
|
79
|
-
* Typical wiring (inside the agent package — see [`agent.ts`](agent.ts:1)):
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* const guarded = applyLoopGuard(merged, { enabled: config.loopGuard !== false })
|
|
83
|
-
* return applyPermission(guarded, config.permission, config.askHandler)
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export const applyLoopGuard = (
|
|
87
|
-
tools: ExecutableTools,
|
|
88
|
-
config: LoopGuardConfig = DEFAULT_CONFIG,
|
|
89
|
-
): ExecutableTools => {
|
|
90
|
-
if (!config.enabled) return tools
|
|
91
|
-
const seen = new Set<string>()
|
|
92
|
-
const out: Record<string, AnyExecutableTool> = {}
|
|
93
|
-
for (const name of Object.keys(tools)) {
|
|
94
|
-
const tool = tools[name]
|
|
95
|
-
if (!tool) continue
|
|
96
|
-
out[name] = wrapTool(name, tool, seen)
|
|
97
|
-
}
|
|
98
|
-
return out as ExecutableTools
|
|
99
|
-
}
|
package/src/mcp/client.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/opencode/src/mcp/
|
|
2
|
-
|
|
3
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
4
|
-
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
|
5
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
|
6
|
-
import { Effect } from "effect"
|
|
7
|
-
import { Tool, ToolFailure } from "@tinyweb-agent-framework/llm"
|
|
8
|
-
|
|
9
|
-
// ---------------------------------------------------------------------------
|
|
10
|
-
// Config types
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
|
|
13
|
-
export type McpStdioConfig = {
|
|
14
|
-
readonly type: "stdio"
|
|
15
|
-
readonly command: string
|
|
16
|
-
readonly args?: string[]
|
|
17
|
-
readonly env?: Record<string, string>
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type McpHttpConfig = {
|
|
21
|
-
readonly type: "http"
|
|
22
|
-
readonly url: string
|
|
23
|
-
readonly headers?: Record<string, string>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type McpServerConfig = McpStdioConfig | McpHttpConfig
|
|
27
|
-
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
// McpClient — holds connected client + tools
|
|
30
|
-
// ---------------------------------------------------------------------------
|
|
31
|
-
|
|
32
|
-
export type McpClient = {
|
|
33
|
-
readonly name: string
|
|
34
|
-
readonly tools: Record<string, Tool.AnyExecutableTool>
|
|
35
|
-
readonly disconnect: () => Promise<void>
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// ---------------------------------------------------------------------------
|
|
39
|
-
// Helpers
|
|
40
|
-
// ---------------------------------------------------------------------------
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* MCP tools ship a real JSON Schema (`inputSchema`). We must pass it through
|
|
44
|
-
* `Tool.make({ jsonSchema })` so the LLM sees required fields (e.g. Perplexity
|
|
45
|
-
* `messages[]` / `query`). Using a generic Record schema loses that shape and
|
|
46
|
-
* the model invents empty `{}` args → server-side validation errors.
|
|
47
|
-
*/
|
|
48
|
-
function asJsonSchema(inputSchema: unknown): Record<string, unknown> {
|
|
49
|
-
if (inputSchema && typeof inputSchema === "object" && !Array.isArray(inputSchema)) {
|
|
50
|
-
return inputSchema as Record<string, unknown>
|
|
51
|
-
}
|
|
52
|
-
return { type: "object", properties: {} }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function buildTool(
|
|
56
|
-
serverName: string,
|
|
57
|
-
mcpTool: { name: string; description?: string; inputSchema: unknown },
|
|
58
|
-
client: Client,
|
|
59
|
-
): Tool.AnyExecutableTool {
|
|
60
|
-
const toolName = `${serverName}__${mcpTool.name}`
|
|
61
|
-
const jsonSchema = asJsonSchema(mcpTool.inputSchema)
|
|
62
|
-
|
|
63
|
-
return Tool.make({
|
|
64
|
-
description: mcpTool.description ?? toolName,
|
|
65
|
-
// Dynamic mode — preserves MCP inputSchema on the wire for the LLM.
|
|
66
|
-
jsonSchema: jsonSchema as never,
|
|
67
|
-
execute: (input) =>
|
|
68
|
-
Effect.tryPromise({
|
|
69
|
-
try: async () => {
|
|
70
|
-
const args =
|
|
71
|
-
input && typeof input === "object" && !Array.isArray(input)
|
|
72
|
-
? (input as Record<string, unknown>)
|
|
73
|
-
: {}
|
|
74
|
-
const result = await client.callTool({ name: mcpTool.name, arguments: args })
|
|
75
|
-
// MCP callTool returns {content: [{type, text?}], isError?}
|
|
76
|
-
const parts = (result.content as { type: string; text?: string }[] | undefined) ?? []
|
|
77
|
-
const text = parts.map((c) => c.text ?? "").join("\n")
|
|
78
|
-
if (result.isError) {
|
|
79
|
-
throw new Error(text || "MCP tool returned error")
|
|
80
|
-
}
|
|
81
|
-
return text
|
|
82
|
-
},
|
|
83
|
-
catch: (err) => new ToolFailure({ message: String(err) }),
|
|
84
|
-
}),
|
|
85
|
-
}) as Tool.AnyExecutableTool
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// ---------------------------------------------------------------------------
|
|
89
|
-
// connectMcp
|
|
90
|
-
// ---------------------------------------------------------------------------
|
|
91
|
-
|
|
92
|
-
export async function connectMcp(name: string, config: McpServerConfig): Promise<McpClient> {
|
|
93
|
-
const client = new Client({ name: "tinyweb-agent", version: "0.0.1" })
|
|
94
|
-
|
|
95
|
-
const transport =
|
|
96
|
-
config.type === "stdio"
|
|
97
|
-
? new StdioClientTransport({ command: config.command, args: config.args, env: config.env })
|
|
98
|
-
: new StreamableHTTPClientTransport(new URL(config.url), { requestInit: { headers: config.headers ?? {} } })
|
|
99
|
-
|
|
100
|
-
await client.connect(transport)
|
|
101
|
-
|
|
102
|
-
const listResult = await client.listTools()
|
|
103
|
-
const tools: Record<string, Tool.AnyExecutableTool> = {}
|
|
104
|
-
for (const mcpTool of listResult.tools) {
|
|
105
|
-
const namespaced = `${name}__${mcpTool.name}`
|
|
106
|
-
tools[namespaced] = buildTool(name, mcpTool, client)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
name,
|
|
111
|
-
tools,
|
|
112
|
-
disconnect: () => client.close(),
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// ---------------------------------------------------------------------------
|
|
117
|
-
// connectAllMcp — connect multiple servers, merge tools
|
|
118
|
-
// ---------------------------------------------------------------------------
|
|
119
|
-
|
|
120
|
-
export type McpConfig = Record<string, McpServerConfig>
|
|
121
|
-
|
|
122
|
-
export async function connectAllMcp(configs: McpConfig): Promise<{ clients: McpClient[]; tools: Record<string, Tool.AnyExecutableTool> }> {
|
|
123
|
-
const clients = await Promise.all(Object.entries(configs).map(([name, config]) => connectMcp(name, config)))
|
|
124
|
-
const tools: Record<string, Tool.AnyExecutableTool> = {}
|
|
125
|
-
for (const mcp of clients) {
|
|
126
|
-
Object.assign(tools, mcp.tools)
|
|
127
|
-
}
|
|
128
|
-
return { clients, tools }
|
|
129
|
-
}
|
package/src/permission.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { Tool, ToolFailure, type AnyExecutableTool, type ExecutableTools } from "@tinyweb-agent-framework/llm"
|
|
2
|
-
import { Effect } from "effect"
|
|
3
|
-
|
|
4
|
-
export type PermissionRule = "allow" | "ask" | "deny"
|
|
5
|
-
|
|
6
|
-
export type PermissionConfig = PermissionRule | Readonly<Record<string, PermissionRule>>
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* How `"ask"` rules are enforced:
|
|
10
|
-
* - `"handler"` (default, backward-compat) — call `askHandler` and block the
|
|
11
|
-
* tool stream until it resolves. Suitable for in-process UIs / CLIs.
|
|
12
|
-
* - `"interrupt"` — short-circuit the tool with a structured `ToolFailure`
|
|
13
|
-
* (`metadata.kind = "tool-interrupt"`). The agent loop stops, persists a
|
|
14
|
-
* `pending-approval` ToolPart, and the host later calls
|
|
15
|
-
* `agent.resolveApproval` + `agent.resume`. No hanging Promise; survives
|
|
16
|
-
* process restarts when the SessionStore is durable.
|
|
17
|
-
*/
|
|
18
|
-
export type PermissionAskMode = "handler" | "interrupt"
|
|
19
|
-
|
|
20
|
-
export type PermissionAskRequest = {
|
|
21
|
-
readonly toolName: string
|
|
22
|
-
readonly input: unknown
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type PermissionAskHandler = (request: PermissionAskRequest) => Promise<boolean>
|
|
26
|
-
|
|
27
|
-
/** Metadata key written onto ToolFailure / ToolResultValue for interrupts. */
|
|
28
|
-
export const TOOL_INTERRUPT_KIND = "tool-interrupt" as const
|
|
29
|
-
|
|
30
|
-
export const isToolInterruptMetadata = (
|
|
31
|
-
metadata: Record<string, unknown> | undefined | null,
|
|
32
|
-
): boolean => metadata?.kind === TOOL_INTERRUPT_KIND
|
|
33
|
-
|
|
34
|
-
export const toolInterruptFailure = (toolName: string, input: unknown): ToolFailure =>
|
|
35
|
-
new ToolFailure({
|
|
36
|
-
message: `Tool "${toolName}" requires user approval`,
|
|
37
|
-
metadata: {
|
|
38
|
-
kind: TOOL_INTERRUPT_KIND,
|
|
39
|
-
toolName,
|
|
40
|
-
input,
|
|
41
|
-
},
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
export const resolveRule = (config: PermissionConfig | undefined, toolName: string): PermissionRule => {
|
|
45
|
-
if (config === undefined) return "allow"
|
|
46
|
-
if (typeof config === "string") return config
|
|
47
|
-
return config[toolName] ?? "allow"
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const deniedFailure = (toolName: string): ToolFailure =>
|
|
51
|
-
new ToolFailure({ message: `Tool "${toolName}" denied by permission policy` })
|
|
52
|
-
|
|
53
|
-
const askDeniedFailure = (toolName: string): ToolFailure =>
|
|
54
|
-
new ToolFailure({ message: `Tool "${toolName}" denied by permission ask handler` })
|
|
55
|
-
|
|
56
|
-
const wrapTool = (
|
|
57
|
-
name: string,
|
|
58
|
-
base: AnyExecutableTool,
|
|
59
|
-
rule: PermissionRule,
|
|
60
|
-
askHandler: PermissionAskHandler | undefined,
|
|
61
|
-
askMode: PermissionAskMode,
|
|
62
|
-
): AnyExecutableTool => {
|
|
63
|
-
if (rule === "allow") return base
|
|
64
|
-
if (rule === "deny") {
|
|
65
|
-
return Tool.make({
|
|
66
|
-
description: base.description,
|
|
67
|
-
parameters: base.parameters,
|
|
68
|
-
success: base.success,
|
|
69
|
-
execute: () => Effect.fail(deniedFailure(name)),
|
|
70
|
-
}) as AnyExecutableTool
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// rule === "ask"
|
|
74
|
-
if (askMode === "interrupt") {
|
|
75
|
-
return Tool.make({
|
|
76
|
-
description: base.description,
|
|
77
|
-
parameters: base.parameters,
|
|
78
|
-
success: base.success,
|
|
79
|
-
execute: (input: unknown) => Effect.fail(toolInterruptFailure(name, input)),
|
|
80
|
-
}) as AnyExecutableTool
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return Tool.make({
|
|
84
|
-
description: base.description,
|
|
85
|
-
parameters: base.parameters,
|
|
86
|
-
success: base.success,
|
|
87
|
-
execute: (input: unknown) =>
|
|
88
|
-
Effect.gen(function* () {
|
|
89
|
-
const allowed = yield* Effect.tryPromise({
|
|
90
|
-
try: () => (askHandler ? askHandler({ toolName: name, input }) : Promise.resolve(false)),
|
|
91
|
-
catch: (cause) => new ToolFailure({ message: `Permission ask handler threw: ${String(cause)}` }),
|
|
92
|
-
})
|
|
93
|
-
if (!allowed) return yield* Effect.fail(askDeniedFailure(name))
|
|
94
|
-
return yield* base.execute(input)
|
|
95
|
-
}),
|
|
96
|
-
}) as AnyExecutableTool
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export const applyPermission = (
|
|
100
|
-
tools: ExecutableTools,
|
|
101
|
-
config: PermissionConfig | undefined,
|
|
102
|
-
askHandler: PermissionAskHandler | undefined,
|
|
103
|
-
askMode: PermissionAskMode = "handler",
|
|
104
|
-
): ExecutableTools => {
|
|
105
|
-
if (config === undefined) return tools
|
|
106
|
-
const out: Record<string, AnyExecutableTool> = {}
|
|
107
|
-
const names = Object.keys(tools)
|
|
108
|
-
for (const name of names) {
|
|
109
|
-
const tool = tools[name]
|
|
110
|
-
if (!tool) continue
|
|
111
|
-
out[name] = wrapTool(name, tool, resolveRule(config, name), askHandler, askMode)
|
|
112
|
-
}
|
|
113
|
-
return out as ExecutableTools
|
|
114
|
-
}
|
package/src/plugin/apply.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/opencode/src/plugin/index.ts (apply pattern)
|
|
2
|
-
|
|
3
|
-
import type { LoadedPlugin } from "./types"
|
|
4
|
-
import type {
|
|
5
|
-
ChatHeadersInput,
|
|
6
|
-
ChatHeadersOutput,
|
|
7
|
-
ChatMessageInput,
|
|
8
|
-
ChatMessageOutput,
|
|
9
|
-
ChatParamsInput,
|
|
10
|
-
ChatParamsOutput,
|
|
11
|
-
EventHookInput,
|
|
12
|
-
PermissionAskHookInput,
|
|
13
|
-
PermissionAskHookOutput,
|
|
14
|
-
SystemTransformInput,
|
|
15
|
-
SystemTransformOutput,
|
|
16
|
-
ToolDefinitionInput,
|
|
17
|
-
ToolDefinitionOutput,
|
|
18
|
-
ToolExecuteAfterInput,
|
|
19
|
-
ToolExecuteAfterOutput,
|
|
20
|
-
ToolExecuteBeforeInput,
|
|
21
|
-
ToolExecuteBeforeOutput,
|
|
22
|
-
} from "./types"
|
|
23
|
-
|
|
24
|
-
const runHooks = async <I, O>(
|
|
25
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
26
|
-
key: string,
|
|
27
|
-
input: I,
|
|
28
|
-
output: O,
|
|
29
|
-
): Promise<O> => {
|
|
30
|
-
for (const p of plugins) {
|
|
31
|
-
const hook = (p.hooks as Record<string, ((i: I, o: O) => Promise<void>) | undefined>)[key]
|
|
32
|
-
if (hook) await hook(input, output)
|
|
33
|
-
}
|
|
34
|
-
return output
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const applyEvent = async (plugins: ReadonlyArray<LoadedPlugin>, input: EventHookInput): Promise<void> => {
|
|
38
|
-
for (const p of plugins) {
|
|
39
|
-
if (p.hooks.event) await p.hooks.event(input)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const applyChatMessage = (
|
|
44
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
45
|
-
input: ChatMessageInput,
|
|
46
|
-
output: ChatMessageOutput,
|
|
47
|
-
) => runHooks(plugins, "chat.message", input, output)
|
|
48
|
-
|
|
49
|
-
export const applyChatParams = (
|
|
50
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
51
|
-
input: ChatParamsInput,
|
|
52
|
-
output: ChatParamsOutput,
|
|
53
|
-
) => runHooks(plugins, "chat.params", input, output)
|
|
54
|
-
|
|
55
|
-
export const applyChatHeaders = (
|
|
56
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
57
|
-
input: ChatHeadersInput,
|
|
58
|
-
output: ChatHeadersOutput,
|
|
59
|
-
) => runHooks(plugins, "chat.headers", input, output)
|
|
60
|
-
|
|
61
|
-
export const applyToolBefore = (
|
|
62
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
63
|
-
input: ToolExecuteBeforeInput,
|
|
64
|
-
output: ToolExecuteBeforeOutput,
|
|
65
|
-
) => runHooks(plugins, "tool.execute.before", input, output)
|
|
66
|
-
|
|
67
|
-
export const applyToolAfter = (
|
|
68
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
69
|
-
input: ToolExecuteAfterInput,
|
|
70
|
-
output: ToolExecuteAfterOutput,
|
|
71
|
-
) => runHooks(plugins, "tool.execute.after", input, output)
|
|
72
|
-
|
|
73
|
-
export const applyToolDefinition = (
|
|
74
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
75
|
-
input: ToolDefinitionInput,
|
|
76
|
-
output: ToolDefinitionOutput,
|
|
77
|
-
) => runHooks(plugins, "tool.definition", input, output)
|
|
78
|
-
|
|
79
|
-
export const applyPermissionHook = (
|
|
80
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
81
|
-
input: PermissionAskHookInput,
|
|
82
|
-
output: PermissionAskHookOutput,
|
|
83
|
-
) => runHooks(plugins, "permission.ask", input, output)
|
|
84
|
-
|
|
85
|
-
export const applySystemTransform = (
|
|
86
|
-
plugins: ReadonlyArray<LoadedPlugin>,
|
|
87
|
-
input: SystemTransformInput,
|
|
88
|
-
output: SystemTransformOutput,
|
|
89
|
-
) => runHooks(plugins, "experimental.chat.system.transform", input, output)
|
package/src/plugin/index.ts
DELETED
package/src/plugin/loader.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/opencode/src/plugin/index.ts
|
|
2
|
-
|
|
3
|
-
import type { LoadedPlugin, Plugin, PluginEntry, PluginInput, PluginOptions } from "./types"
|
|
4
|
-
|
|
5
|
-
type PluginModule = {
|
|
6
|
-
readonly id?: string
|
|
7
|
-
readonly server?: Plugin
|
|
8
|
-
readonly default?: Plugin
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const isPlugin = (value: unknown): value is Plugin => typeof value === "function"
|
|
12
|
-
|
|
13
|
-
const idFromPath = (path: string) => path.replace(/[^a-z0-9]+/gi, "-").replace(/^-+|-+$/g, "") || "plugin"
|
|
14
|
-
|
|
15
|
-
const importPlugin = async (path: string): Promise<{ id: string; plugin: Plugin }> => {
|
|
16
|
-
const mod = (await import(path)) as PluginModule
|
|
17
|
-
const plugin = mod.server ?? mod.default
|
|
18
|
-
if (!isPlugin(plugin))
|
|
19
|
-
throw new Error(`Plugin at ${path} must export a 'server' or default function returning Hooks`)
|
|
20
|
-
return { id: mod.id ?? idFromPath(path), plugin }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const normalizeEntry = async (entry: PluginEntry, index: number): Promise<{ id: string; plugin: Plugin; options?: PluginOptions }> => {
|
|
24
|
-
if (isPlugin(entry)) return { id: `inline-${index}`, plugin: entry }
|
|
25
|
-
if (typeof entry === "string") {
|
|
26
|
-
const loaded = await importPlugin(entry)
|
|
27
|
-
return { id: loaded.id, plugin: loaded.plugin }
|
|
28
|
-
}
|
|
29
|
-
const loaded = await importPlugin(entry[0])
|
|
30
|
-
return { id: loaded.id, plugin: loaded.plugin, options: entry[1] }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const loadPlugins = async (
|
|
34
|
-
entries: ReadonlyArray<PluginEntry>,
|
|
35
|
-
input: PluginInput,
|
|
36
|
-
): Promise<LoadedPlugin[]> => {
|
|
37
|
-
const resolved = await Promise.all(entries.map((entry, i) => normalizeEntry(entry, i)))
|
|
38
|
-
const loaded = await Promise.all(
|
|
39
|
-
resolved.map(async (item) => ({ id: item.id, hooks: await item.plugin(input, item.options) })),
|
|
40
|
-
)
|
|
41
|
-
return loaded
|
|
42
|
-
}
|
package/src/plugin/types.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/plugin/src/index.ts
|
|
2
|
-
// Trimmed for tinyweb-agent v1 scope: drop auth/provider/command/shell/workspace/compaction hooks.
|
|
3
|
-
|
|
4
|
-
import type { ModelRef, ToolDefinition } from "@tinyweb-agent-framework/llm"
|
|
5
|
-
import type { Message, UserMessage } from "../session/schema"
|
|
6
|
-
import type { BusEvent } from "../bus/bus"
|
|
7
|
-
|
|
8
|
-
export type PluginOptions = Readonly<Record<string, unknown>>
|
|
9
|
-
|
|
10
|
-
export type PluginInput = {
|
|
11
|
-
readonly agentName: string
|
|
12
|
-
readonly directory: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type ChatParamsInput = {
|
|
16
|
-
readonly sessionId: string
|
|
17
|
-
readonly agentName: string
|
|
18
|
-
readonly model: ModelRef
|
|
19
|
-
readonly message: UserMessage
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type ChatParamsOutput = {
|
|
23
|
-
temperature?: number
|
|
24
|
-
topP?: number
|
|
25
|
-
topK?: number
|
|
26
|
-
maxOutputTokens?: number
|
|
27
|
-
options: Record<string, unknown>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type ChatHeadersInput = ChatParamsInput
|
|
31
|
-
|
|
32
|
-
export type ChatHeadersOutput = {
|
|
33
|
-
headers: Record<string, string>
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type ChatMessageInput = {
|
|
37
|
-
readonly sessionId: string
|
|
38
|
-
readonly agentName: string
|
|
39
|
-
readonly model: ModelRef
|
|
40
|
-
readonly messageId: string
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type ChatMessageOutput = {
|
|
44
|
-
message: UserMessage
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export type ToolExecuteBeforeInput = {
|
|
48
|
-
readonly tool: string
|
|
49
|
-
readonly sessionId: string
|
|
50
|
-
readonly callId: string
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type ToolExecuteBeforeOutput = {
|
|
54
|
-
args: unknown
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type ToolExecuteAfterInput = {
|
|
58
|
-
readonly tool: string
|
|
59
|
-
readonly sessionId: string
|
|
60
|
-
readonly callId: string
|
|
61
|
-
readonly args: unknown
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type ToolExecuteAfterOutput = {
|
|
65
|
-
output: unknown
|
|
66
|
-
metadata: Record<string, unknown>
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export type ToolDefinitionInput = {
|
|
70
|
-
readonly toolId: string
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export type ToolDefinitionOutput = {
|
|
74
|
-
description: string
|
|
75
|
-
parameters: ToolDefinition["inputSchema"]
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export type PermissionAskHookInput = {
|
|
79
|
-
readonly toolName: string
|
|
80
|
-
readonly sessionId: string
|
|
81
|
-
readonly callId: string
|
|
82
|
-
readonly input: unknown
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export type PermissionAskHookOutput = {
|
|
86
|
-
status: "allow" | "ask" | "deny"
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export type SystemTransformInput = {
|
|
90
|
-
readonly sessionId: string
|
|
91
|
-
readonly agentName: string
|
|
92
|
-
readonly model: ModelRef
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export type SystemTransformOutput = {
|
|
96
|
-
system: string[]
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export type EventHookInput = {
|
|
100
|
-
readonly event: BusEvent
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export type Hooks = {
|
|
104
|
-
readonly event?: (input: EventHookInput) => Promise<void>
|
|
105
|
-
readonly "chat.message"?: (input: ChatMessageInput, output: ChatMessageOutput) => Promise<void>
|
|
106
|
-
readonly "chat.params"?: (input: ChatParamsInput, output: ChatParamsOutput) => Promise<void>
|
|
107
|
-
readonly "chat.headers"?: (input: ChatHeadersInput, output: ChatHeadersOutput) => Promise<void>
|
|
108
|
-
readonly "tool.execute.before"?: (
|
|
109
|
-
input: ToolExecuteBeforeInput,
|
|
110
|
-
output: ToolExecuteBeforeOutput,
|
|
111
|
-
) => Promise<void>
|
|
112
|
-
readonly "tool.execute.after"?: (
|
|
113
|
-
input: ToolExecuteAfterInput,
|
|
114
|
-
output: ToolExecuteAfterOutput,
|
|
115
|
-
) => Promise<void>
|
|
116
|
-
readonly "tool.definition"?: (input: ToolDefinitionInput, output: ToolDefinitionOutput) => Promise<void>
|
|
117
|
-
readonly "permission.ask"?: (input: PermissionAskHookInput, output: PermissionAskHookOutput) => Promise<void>
|
|
118
|
-
readonly "experimental.chat.system.transform"?: (
|
|
119
|
-
input: SystemTransformInput,
|
|
120
|
-
output: SystemTransformOutput,
|
|
121
|
-
) => Promise<void>
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export type Plugin = (input: PluginInput, options?: PluginOptions) => Promise<Hooks>
|
|
125
|
-
|
|
126
|
-
export type PluginEntry = Plugin | string | readonly [string, PluginOptions]
|
|
127
|
-
|
|
128
|
-
export type LoadedPlugin = {
|
|
129
|
-
readonly id: string
|
|
130
|
-
readonly hooks: Hooks
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Re-export Message for plugin consumers that want to inspect history.
|
|
134
|
-
export type { Message }
|