MCPcopy Index your code
hub / github.com/anomalyco/opencode / wrap

Function wrap

packages/opencode/src/tool/tool.ts:99–149  ·  view source on GitHub ↗
(
  id: string,
  init: Init<Parameters, Result>,
  truncate: Truncate.Interface,
  agents: Agent.Interface,
)

Source from the content-addressed store, hash-verified

97 : never
98
99function wrap<Parameters extends Schema.Decoder<unknown>, Result extends Metadata>(
100 id: string,
101 init: Init<Parameters, Result>,
102 truncate: Truncate.Interface,
103 agents: Agent.Interface,
104) {
105 return () =>
106 Effect.gen(function* () {
107 const toolInfo = typeof init === "function" ? { ...(yield* init()) } : { ...init }
108 // Compile the parser closure once per tool init; `decodeUnknownEffect`
109 // allocates a new closure per call, so hoisting avoids re-closing it for
110 // every LLM tool invocation.
111 const decode = Schema.decodeUnknownEffect(toolInfo.parameters)
112 const execute = toolInfo.execute
113 toolInfo.execute = (args, ctx) => {
114 const attrs = {
115 "tool.name": id,
116 "session.id": ctx.sessionID,
117 "message.id": ctx.messageID,
118 ...(ctx.callID ? { "tool.call_id": ctx.callID } : {}),
119 }
120 return Effect.gen(function* () {
121 const decoded = yield* decode(args).pipe(
122 Effect.mapError(
123 (error) =>
124 new InvalidArgumentsError({
125 tool: id,
126 detail: toolInfo.formatValidationError ? toolInfo.formatValidationError(error) : String(error),
127 }),
128 ),
129 )
130 const result = yield* execute(decoded as Schema.Schema.Type<Parameters>, ctx)
131 if (result.metadata.truncated !== undefined) {
132 return result
133 }
134 const agent = yield* agents.get(ctx.agent)
135 const truncated = yield* truncate.output(result.output, {}, agent)
136 return {
137 ...result,
138 output: truncated.content,
139 metadata: {
140 ...result.metadata,
141 truncated: truncated.truncated,
142 ...(truncated.truncated && { outputPath: truncated.outputPath }),
143 },
144 }
145 }).pipe(Effect.orDie, Effect.withSpan("Tool.execute", { attributes: attrs }))
146 }
147 return toolInfo
148 })
149}
150
151export function define<
152 Parameters extends Schema.Decoder<unknown>,

Callers 2

defineFunction · 0.70

Calls 5

formatValidationErrorMethod · 0.80
initFunction · 0.70
getMethod · 0.65
decodeFunction · 0.50
executeFunction · 0.50

Tested by

no test coverage detected