@tinyweb_dev/agent-framework-agent 0.1.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.
Files changed (144) hide show
  1. package/dist/agent.d.ts +116 -0
  2. package/dist/agent.d.ts.map +1 -0
  3. package/dist/attachments.d.ts +41 -0
  4. package/dist/attachments.d.ts.map +1 -0
  5. package/dist/bus/bus.d.ts +46 -0
  6. package/dist/bus/bus.d.ts.map +1 -0
  7. package/dist/bus/index.d.ts +2 -0
  8. package/dist/bus/index.d.ts.map +1 -0
  9. package/dist/define-tool.d.ts +13 -0
  10. package/dist/define-tool.d.ts.map +1 -0
  11. package/dist/events.d.ts +136 -0
  12. package/dist/events.d.ts.map +1 -0
  13. package/dist/http-handlers.cjs +253 -0
  14. package/dist/http-handlers.cjs.map +1 -0
  15. package/dist/http-handlers.d.ts +49 -0
  16. package/dist/http-handlers.d.ts.map +1 -0
  17. package/dist/http-handlers.js +251 -0
  18. package/dist/http-handlers.js.map +1 -0
  19. package/dist/index.cjs +2073 -0
  20. package/dist/index.cjs.map +1 -0
  21. package/dist/index.d.ts +19 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +2015 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/loop-guard.d.ts +42 -0
  26. package/dist/loop-guard.d.ts.map +1 -0
  27. package/dist/mcp/client.d.ts +25 -0
  28. package/dist/mcp/client.d.ts.map +1 -0
  29. package/dist/mcp/index.cjs +66 -0
  30. package/dist/mcp/index.cjs.map +1 -0
  31. package/{src/mcp/index.ts → dist/mcp/index.d.ts} +2 -1
  32. package/dist/mcp/index.d.ts.map +1 -0
  33. package/dist/mcp/index.js +63 -0
  34. package/dist/mcp/index.js.map +1 -0
  35. package/dist/permission.d.ts +26 -0
  36. package/dist/permission.d.ts.map +1 -0
  37. package/dist/plugin/apply.d.ts +12 -0
  38. package/dist/plugin/apply.d.ts.map +1 -0
  39. package/dist/plugin/index.cjs +63 -0
  40. package/dist/plugin/index.cjs.map +1 -0
  41. package/dist/plugin/index.d.ts +4 -0
  42. package/dist/plugin/index.d.ts.map +1 -0
  43. package/dist/plugin/index.js +52 -0
  44. package/dist/plugin/index.js.map +1 -0
  45. package/dist/plugin/loader.d.ts +3 -0
  46. package/dist/plugin/loader.d.ts.map +1 -0
  47. package/dist/plugin/types.d.ts +98 -0
  48. package/dist/plugin/types.d.ts.map +1 -0
  49. package/dist/prompt/index.d.ts +51 -0
  50. package/dist/prompt/index.d.ts.map +1 -0
  51. package/dist/prompt/objective.d.ts +29 -0
  52. package/dist/prompt/objective.d.ts.map +1 -0
  53. package/dist/prompt/response-style.d.ts +19 -0
  54. package/dist/prompt/response-style.d.ts.map +1 -0
  55. package/dist/prompt/tool-use-guidelines.d.ts +22 -0
  56. package/dist/prompt/tool-use-guidelines.d.ts.map +1 -0
  57. package/dist/prompt/tool-use.d.ts +12 -0
  58. package/dist/prompt/tool-use.d.ts.map +1 -0
  59. package/dist/runtime.d.ts +7 -0
  60. package/dist/runtime.d.ts.map +1 -0
  61. package/dist/session/adapter.d.ts +40 -0
  62. package/dist/session/adapter.d.ts.map +1 -0
  63. package/dist/session/compaction.d.ts +37 -0
  64. package/dist/session/compaction.d.ts.map +1 -0
  65. package/dist/session/index.d.ts +7 -0
  66. package/dist/session/index.d.ts.map +1 -0
  67. package/dist/session/memory.cjs +130 -0
  68. package/dist/session/memory.cjs.map +1 -0
  69. package/dist/session/memory.d.ts +3 -0
  70. package/dist/session/memory.d.ts.map +1 -0
  71. package/dist/session/memory.js +128 -0
  72. package/dist/session/memory.js.map +1 -0
  73. package/dist/session/overflow.d.ts +19 -0
  74. package/dist/session/overflow.d.ts.map +1 -0
  75. package/dist/session/postgres.cjs +245 -0
  76. package/dist/session/postgres.cjs.map +1 -0
  77. package/dist/session/postgres.d.ts +10 -0
  78. package/dist/session/postgres.d.ts.map +1 -0
  79. package/dist/session/postgres.js +243 -0
  80. package/dist/session/postgres.js.map +1 -0
  81. package/dist/session/processor.d.ts +52 -0
  82. package/dist/session/processor.d.ts.map +1 -0
  83. package/dist/session/replay.d.ts +4 -0
  84. package/dist/session/replay.d.ts.map +1 -0
  85. package/dist/session/schema.d.ts +104 -0
  86. package/dist/session/schema.d.ts.map +1 -0
  87. package/dist/session/sqlite.cjs +236 -0
  88. package/dist/session/sqlite.cjs.map +1 -0
  89. package/dist/session/sqlite.d.ts +3 -0
  90. package/dist/session/sqlite.d.ts.map +1 -0
  91. package/dist/session/sqlite.js +234 -0
  92. package/dist/session/sqlite.js.map +1 -0
  93. package/dist/session-api.cjs +56 -0
  94. package/dist/session-api.cjs.map +1 -0
  95. package/dist/session-api.d.ts +36 -0
  96. package/dist/session-api.d.ts.map +1 -0
  97. package/dist/session-api.js +54 -0
  98. package/dist/session-api.js.map +1 -0
  99. package/dist/skill/index.cjs +81 -0
  100. package/dist/skill/index.cjs.map +1 -0
  101. package/dist/skill/index.d.ts +10 -0
  102. package/dist/skill/index.d.ts.map +1 -0
  103. package/dist/skill/index.js +78 -0
  104. package/dist/skill/index.js.map +1 -0
  105. package/dist/sse-handler.d.ts +27 -0
  106. package/dist/sse-handler.d.ts.map +1 -0
  107. package/dist/transcript.d.ts +81 -0
  108. package/dist/transcript.d.ts.map +1 -0
  109. package/package.json +54 -19
  110. package/src/agent.ts +0 -317
  111. package/src/attachments.ts +0 -155
  112. package/src/bus/bus.ts +0 -89
  113. package/src/bus/index.ts +0 -1
  114. package/src/define-tool.ts +0 -41
  115. package/src/events.ts +0 -162
  116. package/src/http-handlers.ts +0 -251
  117. package/src/index.ts +0 -120
  118. package/src/loop-guard.ts +0 -99
  119. package/src/mcp/client.ts +0 -110
  120. package/src/permission.ts +0 -72
  121. package/src/plugin/apply.ts +0 -89
  122. package/src/plugin/index.ts +0 -3
  123. package/src/plugin/loader.ts +0 -42
  124. package/src/plugin/types.ts +0 -134
  125. package/src/prompt/index.ts +0 -90
  126. package/src/prompt/objective.ts +0 -104
  127. package/src/prompt/response-style.ts +0 -59
  128. package/src/prompt/tool-use-guidelines.ts +0 -57
  129. package/src/prompt/tool-use.ts +0 -27
  130. package/src/runtime.ts +0 -22
  131. package/src/session/adapter.ts +0 -37
  132. package/src/session/compaction.ts +0 -160
  133. package/src/session/index.ts +0 -11
  134. package/src/session/memory.ts +0 -154
  135. package/src/session/overflow.ts +0 -50
  136. package/src/session/postgres.ts +0 -330
  137. package/src/session/processor.ts +0 -500
  138. package/src/session/replay.ts +0 -140
  139. package/src/session/schema.ts +0 -140
  140. package/src/session/sqlite.ts +0 -281
  141. package/src/session-api.ts +0 -103
  142. package/src/skill/index.ts +0 -113
  143. package/src/sse-handler.ts +0 -67
  144. package/src/transcript.ts +0 -126
@@ -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)
@@ -1,3 +0,0 @@
1
- export * from "./types"
2
- export * from "./loader"
3
- export * from "./apply"
@@ -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
- }
@@ -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 }
@@ -1,90 +0,0 @@
1
- /**
2
- * Framework-owned system-prompt section composer.
3
- *
4
- * Pattern: Roo Code extension / OpenCode build the final system prompt by
5
- * concatenating named, `====`-divided sections. Each section has a single
6
- * responsibility (TOOL USE, GUIDELINES, RESPONSE STYLE) and is authored in
7
- * the framework — application developers only contribute their ROLE / domain
8
- * block.
9
- *
10
- * Public flow (consumed by `defineAgent` in [`agent.ts`](../agent.ts:1)):
11
- *
12
- * const finalPrompt = composeSystemPrompt({
13
- * role: userProvidedRolePrompt,
14
- * sections: { toolUseGuidelines: { maxCallsPerTurn: 3 } },
15
- * })
16
- *
17
- * Order is fixed and intentional:
18
- * 1. ROLE ← user-authored persona/domain prompt
19
- * 2. TOOL USE ← framework-owned (how the tool API works)
20
- * 3. TOOL USE GUIDE. ← framework-owned (when to pick which tool)
21
- * 4. OBJECTIVE ← framework-owned (proactive helpfulness mindset)
22
- * 5. RESPONSE STYLE ← framework-owned (no-repeat-card etc.)
23
- *
24
- * Each section can be individually disabled (`enabled: false`) for advanced
25
- * consumers who want full control. By default all framework sections are on.
26
- */
27
-
28
- import { toolUseSection } from "./tool-use"
29
- import { toolUseGuidelinesSection, type ToolUseGuidelinesConfig } from "./tool-use-guidelines"
30
- import { objectiveSection, type ObjectiveConfig, type ObjectiveKind } from "./objective"
31
- import { responseStyleSection, type ResponseStyleConfig, type ResponseStyleKind } from "./response-style"
32
-
33
- export { toolUseSection, toolUseGuidelinesSection, objectiveSection, responseStyleSection }
34
- export type { ToolUseGuidelinesConfig, ObjectiveConfig, ObjectiveKind, ResponseStyleConfig, ResponseStyleKind }
35
-
36
- export type SectionToggle<T> = boolean | T
37
-
38
- export type PromptSectionsConfig = {
39
- /** TOOL USE block (how tool calls work). Default: enabled. */
40
- readonly toolUse?: boolean
41
- /** TOOL USE GUIDELINES (anti-loop, intent-matching). Default: enabled. */
42
- readonly toolUseGuidelines?: SectionToggle<ToolUseGuidelinesConfig>
43
- /** OBJECTIVE block (proactive helpfulness, no-bail mindset). Default: enabled with kind="chat". */
44
- readonly objective?: SectionToggle<ObjectiveConfig>
45
- /** RESPONSE STYLE block. Default: enabled with kind="chat". */
46
- readonly responseStyle?: SectionToggle<ResponseStyleConfig>
47
- }
48
-
49
- const resolveToggle = <T>(toggle: SectionToggle<T> | undefined, defaults: T): T | undefined => {
50
- if (toggle === false) return undefined
51
- if (toggle === undefined || toggle === true) return defaults
52
- return toggle
53
- }
54
-
55
- export type ComposeSystemPromptInput = {
56
- /** User-authored ROLE / domain prompt. Inserted first, verbatim. */
57
- readonly role?: string
58
- /** Per-section toggles. Omit for default framework behavior. */
59
- readonly sections?: PromptSectionsConfig
60
- }
61
-
62
- export const composeSystemPrompt = (input: ComposeSystemPromptInput): string => {
63
- const parts: string[] = []
64
- const role = input.role?.trim()
65
- if (role && role.length > 0) parts.push(role)
66
-
67
- const sections = input.sections ?? {}
68
-
69
- if (sections.toolUse !== false) {
70
- parts.push(toolUseSection())
71
- }
72
-
73
- const guidelinesCfg = resolveToggle<ToolUseGuidelinesConfig>(sections.toolUseGuidelines, {})
74
- if (guidelinesCfg) {
75
- parts.push(toolUseGuidelinesSection(guidelinesCfg))
76
- }
77
-
78
- const objectiveCfg = resolveToggle<ObjectiveConfig>(sections.objective, {})
79
- if (objectiveCfg) {
80
- parts.push(objectiveSection(objectiveCfg))
81
- }
82
-
83
- const styleCfg = resolveToggle<ResponseStyleConfig>(sections.responseStyle, {})
84
- if (styleCfg) {
85
- const rendered = responseStyleSection(styleCfg)
86
- if (rendered.length > 0) parts.push(rendered)
87
- }
88
-
89
- return parts.join("\n\n")
90
- }
@@ -1,104 +0,0 @@
1
- /**
2
- * OBJECTIVE section — framework-owned block that defines HOW the agent
3
- * approaches each user turn. Counter-balances TOOL USE GUIDELINES (which
4
- * is anti-loop / restrictive) with a proactive mindset:
5
- *
6
- * "Try to help. Don't give up early. If you don't know, search."
7
- *
8
- * Pattern adapted from Roo Code extension:
9
- * roo-code-extension/src/core/prompts/sections/objective.ts
10
- *
11
- * Roo's original is tuned for autonomous code agents (multi-step task
12
- * completion with `attempt_completion`). This version is rewritten for
13
- * conversational chat assistants — the primary failure mode we see is
14
- * not over-tool-calling, but the opposite: the model bailing with
15
- * "I don't have updated info" when a search tool is right there.
16
- *
17
- * Two modes:
18
- * - "chat" (default) — proactive helpfulness, search when knowledge is
19
- * stale or unknown, ask only when truly ambiguous.
20
- * - "task" — Roo-style iterative task completion (kept for parity, useful
21
- * for pilot/automation agents that drive devices or codebases).
22
- */
23
-
24
- export type ObjectiveKind = "chat" | "task"
25
-
26
- export type ObjectiveConfig = {
27
- /** Default "chat". Use "task" for autonomous multi-step agents. */
28
- readonly kind?: ObjectiveKind
29
- }
30
-
31
- const chatObjective = (): string =>
32
- [
33
- "====",
34
- "",
35
- "OBJECTIVE",
36
- "",
37
- "Your job is to be GENUINELY HELPFUL to the user. Treat every question as",
38
- "something you want to answer well — not something to deflect.",
39
- "",
40
- "1. UNDERSTAND the user's actual intent before responding. Re-read the last",
41
- " few turns of conversation; questions like \"giám đốc là ai?\" or \"thì sao?\"",
42
- " only make sense in context of the prior topic. Resolve pronouns and",
43
- " topic references from history before deciding how to answer.",
44
- "",
45
- "2. DECIDE: can you answer well from your own knowledge?",
46
- " - YES → answer directly, concisely, in the user's language.",
47
- " - NO, or the answer needs FRESH data (people, prices, current events,",
48
- " specific companies, recent news) → use the most relevant tool to find",
49
- " out. Do NOT bail with \"I don't have updated info\" when a search tool",
50
- " is available. Search first, then answer with what you found.",
51
- "",
52
- "3. BE PROACTIVE, NOT LAZY:",
53
- " - If the user asks about a specific entity (a company, person, product)",
54
- " and you are not confident your knowledge is current, search.",
55
- " - If a search returns partial info, share what you found and explicitly",
56
- " state what is still unknown — don't pretend nothing was found.",
57
- " - Follow-up questions inherit the topic of the previous turn. \"giám đốc",
58
- " là ai?\" right after a question about company X means \"who is X's CEO?\"",
59
- " — search for that, don't reply \"I don't know which company\".",
60
- "",
61
- "4. ASK ONLY WHEN TRULY AMBIGUOUS. A clarifying question is appropriate when",
62
- " the request is missing information you cannot infer (e.g. user says",
63
- " \"book a flight\" with no destination). It is NOT appropriate as an escape",
64
- " hatch when you simply didn't try to find the answer.",
65
- "",
66
- "5. RESPECT THE TOOL CAPS in TOOL USE GUIDELINES — but use the budget you",
67
- " have. If you have 3 tool calls allowed and you've used 0, and the user's",
68
- " question clearly needs a search, USE one. The cap is a ceiling, not a",
69
- " target to avoid.",
70
- "",
71
- "6. END EACH TURN with a complete, useful answer. Don't stop mid-thought,",
72
- " don't promise to \"check later\", don't return only a tool result without",
73
- " summarizing it in plain language for the user.",
74
- ].join("\n")
75
-
76
- const taskObjective = (): string =>
77
- [
78
- "====",
79
- "",
80
- "OBJECTIVE",
81
- "",
82
- "You accomplish a given task iteratively, breaking it down into clear steps",
83
- "and working through them methodically.",
84
- "",
85
- "1. Analyze the user's task and set clear, achievable goals. Prioritize them",
86
- " in a logical order.",
87
- "2. Work through goals sequentially, using tools one at a time. Each tool",
88
- " call should be informed by the previous result.",
89
- "3. Before calling a tool, analyze the context. Determine which tool is most",
90
- " relevant and whether all required parameters are present or inferable.",
91
- " If a required parameter is missing and cannot be inferred, ask the user",
92
- " instead of guessing.",
93
- "4. When the task is complete, present the final result clearly. Do not",
94
- " continue with extra tool calls \"to be safe\".",
95
- "5. If the user provides feedback, use it to improve and try again — but do",
96
- " not engage in pointless back-and-forth. Don't end responses with open",
97
- " offers for further assistance.",
98
- ].join("\n")
99
-
100
- export const objectiveSection = (config: ObjectiveConfig = {}): string => {
101
- const kind = config.kind ?? "chat"
102
- if (kind === "task") return taskObjective()
103
- return chatObjective()
104
- }
@@ -1,59 +0,0 @@
1
- /**
2
- * RESPONSE STYLE section — framework-owned block that governs HOW the
3
- * assistant phrases its final answer after a tool call completes.
4
- *
5
- * Default flavor: chat-style. The host UI already renders tool result cards
6
- * with structured data, so the model should NOT repeat numbers/names that
7
- * are already visible — it should add at most one line of commentary.
8
- *
9
- * The full text is suppressed when `kind === "off"`. When `kind === "verbose"`
10
- * we drop the no-repeat constraint (useful for code-agent / pilot flows where
11
- * the model needs to narrate every step).
12
- */
13
- export type ResponseStyleKind = "chat" | "verbose" | "off"
14
-
15
- export type ResponseStyleConfig = {
16
- /** Default: "chat". */
17
- readonly kind?: ResponseStyleKind
18
- }
19
-
20
- const chatStyle = (): string =>
21
- [
22
- "====",
23
- "",
24
- "RESPONSE STYLE",
25
- "",
26
- "Your replies are rendered next to a tool-result card that already shows",
27
- "structured data to the user. Therefore:",
28
- "",
29
- "- Do NOT repeat numbers, names, dates, units, codes, or any field value",
30
- " that is already visible on the tool-result card.",
31
- "- After a tool call, write AT MOST one short sentence of commentary,",
32
- " insight, warning, or follow-up suggestion — something the card does not",
33
- " already say.",
34
- "- If you have nothing useful to add, reply with a single short ack such",
35
- " as \"Here you go.\" / \"Done.\" / \"That's the latest.\" — but never restate",
36
- " the card contents.",
37
- "- If the user asked multiple things in one message and you called several",
38
- " tools, write ONE combined sentence with brief observations — do not list",
39
- " the numbers again.",
40
- "- Match the user's language. Keep replies short and direct.",
41
- ].join("\n")
42
-
43
- const verboseStyle = (): string =>
44
- [
45
- "====",
46
- "",
47
- "RESPONSE STYLE",
48
- "",
49
- "Narrate your reasoning and actions so the user can follow along. Summarise",
50
- "tool results in your own words. Keep paragraphs short. Match the user's",
51
- "language. Do not pad with filler.",
52
- ].join("\n")
53
-
54
- export const responseStyleSection = (config: ResponseStyleConfig = {}): string => {
55
- const kind = config.kind ?? "chat"
56
- if (kind === "off") return ""
57
- if (kind === "verbose") return verboseStyle()
58
- return chatStyle()
59
- }
@@ -1,57 +0,0 @@
1
- /**
2
- * TOOL USE GUIDELINES section — framework-owned block that constrains HOW
3
- * the model should select and chain tools. Designed to prevent the two most
4
- * common failure modes we see in production:
5
- *
6
- * 1. Keyword-trigger looping — calling weather() because the user said
7
- * "city name", even when the actual intent is travel recommendations.
8
- * 2. Same-args retry storms — the model re-invoking a tool with identical
9
- * arguments after a failure (or "to make sure").
10
- *
11
- * Pattern adapted from opencode and Roo Code extension:
12
- * roo-code-extension/src/core/prompts/sections/tool-use-guidelines.ts
13
- *
14
- * The configurable cap (`maxCallsPerTurn`) lets consumers tune aggressiveness
15
- * — pilot/code-agent flows often want 20+, chat assistants want 3.
16
- */
17
- export type ToolUseGuidelinesConfig = {
18
- /** Max distinct tool calls per turn. Default 3 — tuned for chat assistants. */
19
- readonly maxCallsPerTurn?: number
20
- }
21
-
22
- const DEFAULT_MAX_CALLS_PER_TURN = 3
23
-
24
- export const toolUseGuidelinesSection = (config: ToolUseGuidelinesConfig = {}): string => {
25
- const maxCalls = config.maxCallsPerTurn ?? DEFAULT_MAX_CALLS_PER_TURN
26
- return [
27
- "====",
28
- "",
29
- "TOOL USE GUIDELINES",
30
- "",
31
- "1. READ the user's intent before picking a tool. Do NOT pick a tool just",
32
- " because a keyword in the message matches the tool name. Example: if the",
33
- " user asks for travel recommendations about a city, that does NOT mean",
34
- " you should call a weather tool — pick the search/recommendation tool.",
35
- "",
36
- "2. Prefer answering directly when the question is conversational, opinion-",
37
- " based, or covered by your general knowledge. Tools exist for fresh,",
38
- " external, or computed data — not for restating things you already know.",
39
- "",
40
- "3. If multiple tools could plausibly fit, pick the most specific one. If",
41
- " you are not confident, answer directly instead of guessing a tool.",
42
- "",
43
- "4. ANTI-LOOP RULES (strict):",
44
- " - Do NOT call the same tool twice with identical arguments in one turn.",
45
- " - If a tool returns an error, do NOT retry it. Briefly tell the user",
46
- " what failed and stop.",
47
- ` - Hard cap: at most ${maxCalls} tool call(s) per turn. After reaching the`,
48
- " cap you MUST stop calling tools and answer with whatever data you",
49
- " already collected.",
50
- " - Once a tool has returned enough data to answer the user, STOP. Do not",
51
- " call additional tools \"to be safe\" or to corroborate.",
52
- "",
53
- "5. Each tool call must be informed by the previous result. Do not plan a",
54
- " chain of calls up front and execute it blindly — read each result and",
55
- " decide whether the next call is still needed.",
56
- ].join("\n")
57
- }
@@ -1,27 +0,0 @@
1
- /**
2
- * TOOL USE section — framework-owned block that explains the general
3
- * tool-calling contract to the model.
4
- *
5
- * Pattern adapted from opencode and Roo Code extension:
6
- * roo-code-extension/src/core/prompts/sections/tool-use.ts
7
- *
8
- * This block stays generic. Per-tool descriptions come from the tool registry
9
- * itself (LLM provider-native tool spec) and the consumer agent's ROLE block.
10
- */
11
- export const toolUseSection = (): string =>
12
- [
13
- "====",
14
- "",
15
- "TOOL USE",
16
- "",
17
- "You have access to a set of tools provided by the host application. Tools are",
18
- "invoked through the provider-native function-calling mechanism — you do NOT",
19
- "write tool calls as text, JSON, or XML inside your assistant content.",
20
- "",
21
- "- Each tool call must specify a registered tool name and arguments matching",
22
- " that tool's parameter schema.",
23
- "- Tool results are returned as a separate `tool` message in the conversation",
24
- " history. Read the result before deciding the next action.",
25
- "- You may call multiple tools across a single turn when it genuinely helps,",
26
- " but every call must be justified by the user's intent.",
27
- ].join("\n")
package/src/runtime.ts DELETED
@@ -1,22 +0,0 @@
1
- import { LLMClient, RequestExecutor } from "@tinyweb-agent-framework/llm"
2
- import { Layer, ManagedRuntime } from "effect"
3
-
4
- const mainLayer = LLMClient.layer.pipe(Layer.provide(RequestExecutor.defaultLayer))
5
-
6
- export type AgentRuntime = ManagedRuntime.ManagedRuntime<typeof LLMClient.Service, never>
7
-
8
- let cached: AgentRuntime | undefined
9
-
10
- export const createRuntime = (): AgentRuntime => ManagedRuntime.make(mainLayer)
11
-
12
- export const defaultRuntime = (): AgentRuntime => {
13
- if (cached) return cached
14
- cached = createRuntime()
15
- return cached
16
- }
17
-
18
- export const disposeDefaultRuntime = async () => {
19
- if (!cached) return
20
- await cached.dispose()
21
- cached = undefined
22
- }
@@ -1,37 +0,0 @@
1
- import { Effect } from "effect"
2
- import type { Session, CreateSessionInput, Message, MessageID, SessionID } from "./schema"
3
-
4
- export type GetMessagesOpts = {
5
- readonly limit?: number
6
- readonly before?: MessageID
7
- }
8
-
9
- export type ListSessionsOpts = {
10
- readonly limit?: number
11
- /** Opaque cursor returned by previous call's `nextCursor`. */
12
- readonly cursor?: string
13
- readonly agentName?: string
14
- readonly ownerId?: string
15
- }
16
-
17
- export type ListSessionsResult = {
18
- readonly items: ReadonlyArray<Session.Info>
19
- readonly nextCursor?: string
20
- }
21
-
22
- export type SessionPatch = {
23
- readonly title?: string
24
- readonly metadata?: Record<string, unknown>
25
- }
26
-
27
- export type SessionStore = {
28
- createSession(input: CreateSessionInput): Effect.Effect<Session.Info>
29
- getSession(id: SessionID | string): Effect.Effect<Session.Info | undefined>
30
- upsertSession(input: CreateSessionInput & { id?: SessionID | string }): Effect.Effect<Session.Info>
31
- listSessions(opts?: ListSessionsOpts): Effect.Effect<ListSessionsResult>
32
- updateSession(id: SessionID | string, patch: SessionPatch): Effect.Effect<Session.Info | undefined>
33
- appendMessage(sessionId: SessionID | string, msg: Message): Effect.Effect<void>
34
- getMessages(sessionId: SessionID | string, opts?: GetMessagesOpts): Effect.Effect<Message[]>
35
- deleteMessage(sessionId: SessionID | string, messageId: MessageID | string): Effect.Effect<void>
36
- deleteSession(id: SessionID | string): Effect.Effect<void>
37
- }