agentworkshop 0.2.1 → 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 (34) hide show
  1. package/app/plugins/aw-plugins.client.ts +79 -0
  2. package/cli/commands/plugin.mjs +169 -0
  3. package/cli/commands/update.mjs +12 -4
  4. package/docs/plugins.md +127 -0
  5. package/package.json +19 -2
  6. package/scripts/dev-guard.mjs +13 -0
  7. package/scripts/home-bootstrap.mjs +25 -1
  8. package/sdk/api.mjs +101 -0
  9. package/sdk/client.mjs +120 -0
  10. package/sdk/context.mjs +219 -0
  11. package/sdk/examples/sample-insight/client.mjs +23 -0
  12. package/sdk/examples/sample-insight/index.mjs +39 -0
  13. package/sdk/hooks.mjs +103 -0
  14. package/sdk/index.d.mts +135 -0
  15. package/sdk/index.mjs +41 -0
  16. package/sdk/lifecycle.mjs +25 -0
  17. package/server/api/plugins/[name]/[...path].ts +22 -0
  18. package/server/api/plugins/client/[name].get.ts +18 -0
  19. package/server/api/plugins/manifest.get.ts +10 -0
  20. package/server/api/workshop/dcw/lines/[id]/start.post.ts +2 -0
  21. package/server/api/workshop/dcw/lines/[id]/stop.post.ts +2 -0
  22. package/server/data/daqs.json +347 -61
  23. package/server/data/dcw-lines.json +91 -0
  24. package/server/data/dcw-products.json +77 -0
  25. package/server/data/dcw-recipes.json +220 -0
  26. package/server/data/dcw-rollback.json +1 -1
  27. package/server/data/dcw-runs.json +275 -198
  28. package/server/data/dcws.json +231 -0
  29. package/server/data/line-runs.json +74 -14
  30. package/server/plugins/aw-plugins.ts +24 -0
  31. package/server/services/workshop/daq/daq-controller.ts +6 -2
  32. package/server/services/workshop/dcw/dcw-controller.ts +3 -0
  33. package/server/services/workshop/plugins/host.mjs +289 -0
  34. package/server/services/workshop/scene-events.ts +3 -0
@@ -0,0 +1,25 @@
1
+ // ============================================================
2
+ // AgentWorkShop SDK — 生命周期事件清单(宿主触发;单一事实源)
3
+ // ============================================================
4
+
5
+ /** 服务端生命周期事件清单(宿主触发;文档见 docs/plugins.md) */
6
+ export const LIFECYCLE_EVENTS = Object.freeze([
7
+ 'plugin:host:init',
8
+ 'config:changed',
9
+ 'event:*',
10
+ 'daq:sample',
11
+ 'dcw:write',
12
+ 'line:start',
13
+ 'line:stop',
14
+ 'server:close',
15
+ ])
16
+
17
+ /** 客户端生命周期事件清单 */
18
+ export const CLIENT_EVENTS = Object.freeze([
19
+ 'client:init',
20
+ 'event:*',
21
+ 'page:change',
22
+ 'client:destroy',
23
+ ])
24
+
25
+ export default { LIFECYCLE_EVENTS, CLIENT_EVENTS }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * /api/plugins/:name/** —— 插件自注册 API 的转发层(exact-match)。
3
+ * 插件经 ctx.route(method, path, handler) 注册;handler(event) 返回值由 nitro 序列化。
4
+ * 鉴权由插件自行处理(v1 不强制;可经 resolveUser 复用业务鉴权)。
5
+ */
6
+ import { defineEventHandler, createError, readBody } from 'h3'
7
+ import { getPluginHost } from '@/server/services/workshop/plugins/host.mjs'
8
+
9
+ export default defineEventHandler(async (event) => {
10
+ const host = getPluginHost()
11
+ if (!host) throw createError({ statusCode: 503, statusMessage: 'plugin host not ready' })
12
+ const name = String(event.context.params?.name ?? '')
13
+ const path = '/' + (event.context.params?.path ?? '').replace(/^\/+/, '')
14
+ const handler = host.routes.resolve(name, event.method, path)
15
+ if (!handler) {
16
+ throw createError({ statusCode: 404, statusMessage: `plugin route not found: ${event.method} /api/plugins/${name}${path}` })
17
+ }
18
+ // 预读 body 挂到 event(插件 handler 无 h3 导入能力,经 event.awBody 消费)
19
+ const awBody = await readBody(event).catch(() => undefined)
20
+ ;(event as Record<string, unknown>).awBody = awBody
21
+ return handler(event)
22
+ })
@@ -0,0 +1,18 @@
1
+ /**
2
+ * GET /api/plugins/client/:name —— 插件客户端脚本(text/javascript)。
3
+ * 自包含 ESM(无裸导入);客户端 loader 动态 import 后以 setup(ctx) 装载。
4
+ * 信任模型与 aw commands 相同:仅装载自己放入插件目录的可信代码。
5
+ */
6
+ import { defineEventHandler, getRouterParam, setHeader, createError } from 'h3'
7
+ import { readClientScript } from '@/server/services/workshop/plugins/host.mjs'
8
+
9
+ export default defineEventHandler((event) => {
10
+ const name = String(getRouterParam(event, 'name') ?? '').replace(/\.mjs$/, '')
11
+ const r = readClientScript(name)
12
+ if (r.status !== 200) {
13
+ throw createError({ statusCode: r.status, statusMessage: r.status === 404 ? 'plugin client not found' : 'bad request' })
14
+ }
15
+ setHeader(event, 'content-type', r.contentType ?? 'text/javascript; charset=utf-8')
16
+ setHeader(event, 'cache-control', 'no-cache')
17
+ return r.code
18
+ })
@@ -0,0 +1,10 @@
1
+ /**
2
+ * GET /api/plugins/manifest —— 已装载插件清单(免鉴权只读:名称/版本/作用域/路由/是否有客户端)。
3
+ * 客户端 loader 启动期拉取;未登录/服务未装载时返回空数组(前端静默)。
4
+ */
5
+ import { pluginManifest } from '@/server/services/workshop/plugins/host.mjs'
6
+ import { defineEventHandler } from 'h3'
7
+
8
+ export default defineEventHandler(() => {
9
+ return { plugins: pluginManifest() }
10
+ })
@@ -9,6 +9,7 @@ import { bindDcwBroadcast, getDcwController } from '@/server/services/workshop/d
9
9
  import { broadcastSceneEvent } from '@/server/services/workshop/scene-events'
10
10
  import { getActiveLineRun } from '@/server/services/workshop/dcw/line-run'
11
11
  import { recordOps } from '@/server/services/workshop/ops/ops'
12
+ import { emitLineLifecycle } from '@/server/services/workshop/plugins/host.mjs'
12
13
 
13
14
  export default defineApiHandler(async (event) => {
14
15
  const user = resolveUser(event)
@@ -31,5 +32,6 @@ export default defineApiHandler(async (event) => {
31
32
  recipeId: active?.recipeId ?? String(body.recipeId ?? ''),
32
33
  detail: { runId: run.id, recipeId: body.recipeId ?? '' },
33
34
  })
35
+ emitLineLifecycle('line:start', { lineId: id, runId: run.id, recipeId: active?.recipeId ?? String(body.recipeId ?? ''), productName: active?.productName ?? '' })
34
36
  return { run, line: getDcwController().lineState(id) }
35
37
  })
@@ -9,6 +9,7 @@ import { bindDcwBroadcast, getDcwController } from '@/server/services/workshop/d
9
9
  import { broadcastSceneEvent } from '@/server/services/workshop/scene-events'
10
10
  import { getActiveLineRun } from '@/server/services/workshop/dcw/line-run'
11
11
  import { recordOps } from '@/server/services/workshop/ops/ops'
12
+ import { emitLineLifecycle } from '@/server/services/workshop/plugins/host.mjs'
12
13
 
13
14
  export default defineApiHandler(async (event) => {
14
15
  const user = resolveUser(event)
@@ -30,5 +31,6 @@ export default defineApiHandler(async (event) => {
30
31
  recipeId: active?.recipeId ?? '',
31
32
  detail: { runId: active?.runId ?? '' },
32
33
  })
34
+ emitLineLifecycle('line:stop', { lineId: id, runId: active?.runId ?? run?.id ?? '' })
33
35
  return { run, line: getDcwController().lineState(id) }
34
36
  })