@tinyweb_dev/agent-framework-agent 0.1.0 → 0.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.
package/src/permission.ts CHANGED
@@ -5,6 +5,18 @@ export type PermissionRule = "allow" | "ask" | "deny"
5
5
 
6
6
  export type PermissionConfig = PermissionRule | Readonly<Record<string, PermissionRule>>
7
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
+
8
20
  export type PermissionAskRequest = {
9
21
  readonly toolName: string
10
22
  readonly input: unknown
@@ -12,6 +24,23 @@ export type PermissionAskRequest = {
12
24
 
13
25
  export type PermissionAskHandler = (request: PermissionAskRequest) => Promise<boolean>
14
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
+
15
44
  export const resolveRule = (config: PermissionConfig | undefined, toolName: string): PermissionRule => {
16
45
  if (config === undefined) return "allow"
17
46
  if (typeof config === "string") return config
@@ -29,6 +58,7 @@ const wrapTool = (
29
58
  base: AnyExecutableTool,
30
59
  rule: PermissionRule,
31
60
  askHandler: PermissionAskHandler | undefined,
61
+ askMode: PermissionAskMode,
32
62
  ): AnyExecutableTool => {
33
63
  if (rule === "allow") return base
34
64
  if (rule === "deny") {
@@ -39,6 +69,17 @@ const wrapTool = (
39
69
  execute: () => Effect.fail(deniedFailure(name)),
40
70
  }) as AnyExecutableTool
41
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
+
42
83
  return Tool.make({
43
84
  description: base.description,
44
85
  parameters: base.parameters,
@@ -59,6 +100,7 @@ export const applyPermission = (
59
100
  tools: ExecutableTools,
60
101
  config: PermissionConfig | undefined,
61
102
  askHandler: PermissionAskHandler | undefined,
103
+ askMode: PermissionAskMode = "handler",
62
104
  ): ExecutableTools => {
63
105
  if (config === undefined) return tools
64
106
  const out: Record<string, AnyExecutableTool> = {}
@@ -66,7 +108,7 @@ export const applyPermission = (
66
108
  for (const name of names) {
67
109
  const tool = tools[name]
68
110
  if (!tool) continue
69
- out[name] = wrapTool(name, tool, resolveRule(config, name), askHandler)
111
+ out[name] = wrapTool(name, tool, resolveRule(config, name), askHandler, askMode)
70
112
  }
71
113
  return out as ExecutableTools
72
114
  }
@@ -34,4 +34,13 @@ export type SessionStore = {
34
34
  getMessages(sessionId: SessionID | string, opts?: GetMessagesOpts): Effect.Effect<Message[]>
35
35
  deleteMessage(sessionId: SessionID | string, messageId: MessageID | string): Effect.Effect<void>
36
36
  deleteSession(id: SessionID | string): Effect.Effect<void>
37
+ /**
38
+ * Optional in-place message update (used by tool-approval resolve/resume).
39
+ * Stores that omit this fall back to deleteMessage + appendMessage.
40
+ */
41
+ updateMessage?(
42
+ sessionId: SessionID | string,
43
+ messageId: MessageID | string,
44
+ msg: Message,
45
+ ): Effect.Effect<void>
37
46
  }
@@ -6,6 +6,13 @@ export { makeMemoryStore } from "./memory"
6
6
  // when the consumer doesn't actually use them. Import them directly:
7
7
  // import { makeSqliteStore } from "@tinyweb-agent-framework/agent/session/sqlite"
8
8
  // import { makePostgresStore } from "@tinyweb-agent-framework/agent/session/postgres"
9
- export { makeProcessor, type Processor, type ProcessorConfig, type TurnInput } from "./processor"
9
+ export {
10
+ makeProcessor,
11
+ persistMessageUpdate,
12
+ type Processor,
13
+ type ProcessorConfig,
14
+ type TurnInput,
15
+ type ResumeInput,
16
+ } from "./processor"
10
17
  export { estimateTokens, isOverflow } from "./overflow"
11
18
  export { compact, type CompactInput } from "./compaction"
@@ -123,32 +123,47 @@ export const makeMemoryStore = (): SessionStore => {
123
123
  }
124
124
 
125
125
  const deleteMessage = (
126
- sessionId: SessionID | string,
127
- messageId: MessageID | string,
128
- ): Effect.Effect<void> => {
129
- const entry = store.get(sessionId)
130
- if (!entry) return Effect.void
131
- const idx = entry.messages.findIndex((m) => m.id === messageId)
132
- if (idx === -1) return Effect.void
133
- entry.messages.splice(idx, 1)
134
- entry.info = { ...entry.info, updatedAt: Date.now() }
135
- return Effect.void
136
- }
137
-
138
- const deleteSession = (id: SessionID | string): Effect.Effect<void> => {
139
- store.delete(id)
140
- return Effect.void
141
- }
142
-
143
- return {
144
- createSession,
145
- getSession,
146
- upsertSession,
147
- listSessions,
148
- updateSession,
149
- appendMessage,
150
- getMessages,
151
- deleteMessage,
152
- deleteSession,
126
+ sessionId: SessionID | string,
127
+ messageId: MessageID | string,
128
+ ): Effect.Effect<void> => {
129
+ const entry = store.get(sessionId)
130
+ if (!entry) return Effect.void
131
+ const idx = entry.messages.findIndex((m) => m.id === messageId)
132
+ if (idx === -1) return Effect.void
133
+ entry.messages.splice(idx, 1)
134
+ entry.info = { ...entry.info, updatedAt: Date.now() }
135
+ return Effect.void
136
+ }
137
+
138
+ const updateMessage = (
139
+ sessionId: SessionID | string,
140
+ messageId: MessageID | string,
141
+ msg: Message,
142
+ ): Effect.Effect<void> => {
143
+ const entry = store.get(sessionId)
144
+ if (!entry) return Effect.void
145
+ const idx = entry.messages.findIndex((m) => m.id === messageId)
146
+ if (idx === -1) return Effect.void
147
+ entry.messages[idx] = msg
148
+ entry.info = { ...entry.info, updatedAt: Date.now() }
149
+ return Effect.void
150
+ }
151
+
152
+ const deleteSession = (id: SessionID | string): Effect.Effect<void> => {
153
+ store.delete(id)
154
+ return Effect.void
155
+ }
156
+
157
+ return {
158
+ createSession,
159
+ getSession,
160
+ upsertSession,
161
+ listSessions,
162
+ updateSession,
163
+ appendMessage,
164
+ getMessages,
165
+ deleteMessage,
166
+ updateMessage,
167
+ deleteSession,
168
+ }
153
169
  }
154
- }
@@ -289,39 +289,64 @@ export const makePostgresStore = async (
289
289
  }).pipe(Effect.orDie)
290
290
 
291
291
  const deleteMessage = (
292
- sessionId: SessionID | string,
293
- messageId: MessageID | string,
294
- ): Effect.Effect<void> =>
295
- Effect.tryPromise({
296
- try: async () => {
297
- await db
298
- .delete(messages)
299
- .where(and(eq(messages.session_id, sessionId), eq(messages.id, messageId)))
300
- },
301
- catch: (err) => err as Error,
302
- }).pipe(Effect.orDie)
303
-
304
- const deleteSession = (id: SessionID | string): Effect.Effect<void> =>
305
- Effect.tryPromise({
306
- try: async () => {
307
- await db.delete(messages).where(eq(messages.session_id, id))
308
- await db.delete(sessions).where(eq(sessions.id, id))
309
- },
310
- catch: (err) => err as Error,
311
- }).pipe(Effect.orDie)
312
-
313
- return {
314
- createSession,
315
- getSession,
316
- upsertSession,
317
- listSessions,
318
- updateSession,
319
- appendMessage,
320
- getMessages,
321
- deleteMessage,
322
- deleteSession,
292
+ sessionId: SessionID | string,
293
+ messageId: MessageID | string,
294
+ ): Effect.Effect<void> =>
295
+ Effect.tryPromise({
296
+ try: async () => {
297
+ await db
298
+ .delete(messages)
299
+ .where(and(eq(messages.session_id, sessionId), eq(messages.id, messageId)))
300
+ },
301
+ catch: (err) => err as Error,
302
+ }).pipe(Effect.orDie)
303
+
304
+ const updateMessage = (
305
+ sessionId: SessionID | string,
306
+ messageId: MessageID | string,
307
+ msg: Message,
308
+ ): Effect.Effect<void> =>
309
+ Effect.tryPromise({
310
+ try: async () => {
311
+ const msgRow = msg as AssistantMessageRow
312
+ const now = Date.now()
313
+ await db
314
+ .update(messages)
315
+ .set({
316
+ role: msg.role,
317
+ parts: msg as unknown as Record<string, unknown>,
318
+ model_id: msgRow.modelId ?? null,
319
+ provider_id: msgRow.providerId ?? null,
320
+ usage: (msgRow.usage as Record<string, unknown> | undefined) ?? null,
321
+ })
322
+ .where(and(eq(messages.session_id, sessionId), eq(messages.id, messageId)))
323
+ await db.update(sessions).set({ updated_at: now }).where(eq(sessions.id, sessionId))
324
+ },
325
+ catch: (err) => err as Error,
326
+ }).pipe(Effect.orDie)
327
+
328
+ const deleteSession = (id: SessionID | string): Effect.Effect<void> =>
329
+ Effect.tryPromise({
330
+ try: async () => {
331
+ await db.delete(messages).where(eq(messages.session_id, id))
332
+ await db.delete(sessions).where(eq(sessions.id, id))
333
+ },
334
+ catch: (err) => err as Error,
335
+ }).pipe(Effect.orDie)
336
+
337
+ return {
338
+ createSession,
339
+ getSession,
340
+ upsertSession,
341
+ listSessions,
342
+ updateSession,
343
+ appendMessage,
344
+ getMessages,
345
+ deleteMessage,
346
+ updateMessage,
347
+ deleteSession,
348
+ }
323
349
  }
324
- }
325
350
 
326
351
  type AssistantMessageRow = {
327
352
  modelId?: string